1.2. Negative in Region Here is the documentation of all of the functions available in the CSE8AImage library: For this
Posted: Fri May 20, 2022 10:03 am
1.2. Negative in Region Here is the documentation of all of the
functions available in the CSE8AImage library: For this coding
challenge you will be creating an image transformation to turn a
specific part of the image into its negative.
There are 3 files in this section:
CSE8AImage.py - This file SHOULD NOT BE CHANGED. It contains
helper functions that you can use while creating your filter and
writing test cases.
pa6_2.py - This file is where you will define the
negative_region function. To run this file, you should use the
command 'python pa6_1.py' on the terminal
pa6_test.py - This file is where you will do your testing. This
file contains one testing function, and you should add one more
test (instructions below). To run this file, you can use the Run
button.
Function Name: negative_region
Parameter: img - The 2D list representation of an image,
top_left - The (col, row) coordinate of the upper left of the
region to make negative, width - the width of the region to filter,
height - the height of the region to filter
Return: A 2D list representation of the given image with a
negative region starting at top_left.
Description: This filter converts the image into its
photographic negative by returning a new image where the color of
each pixel is computed as follows: Set each component of the color
to be 255 minus the value of the same component in the
corresponding pixel of the original image. Only do this within the
region specified by top_left, width, and height. The region should
begin at the row and column specified by top_left (inclusive). For
example, if a pixel to be changed to negative is (100, 150, 50),
then the resulting pixel should be (155, 105, 205).
Example Result: After defining your function, try running the
test file using the run button or "python pa6_test.py". Note that
if you use the run button, the image will be output to your screen,
but not be saved to your images folder. Writing Test Cases Your
task here is to add ONE test case using an image. This test case
should use one of the other images provided (e.g. Hopper.jpg or
star.png) or you can provide your own image. You should use the
load_img function provided to load your image (take a look at the
first test case to get an idea of how to load an image). To upload
your own image, right click the images directory and select "Upload
here" to upload an image from your computer to Edstem. This test
case should be called test_negative_2(). Don't forget to uncomment
the call to test_negative_2().
pa6_test code:
from CSE8AImage import *
from pa6_2 import *
"""
RGB codes for common colors. You can add on to this
if you want to experiment with different colors.
"""
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
yellow = (255,255,0)
magenta = (255,0,255)
gray = (128,128,128)
purple = (128,0,128)
def test_negative_1():
cat = load_img("images/cat.jpg")
img = negative_region(cat, (110,100), 300, 250)
save_img(img, "images/negative_cat.jpg")
# add test_negative_2:
test_negative_1()
#test_negative_2()
CSE8AImage.py code:
import numpy as np
from PIL import Image
def load_img(filename):
pil_img = Image.open(filename)
arr = np.array(pil_img.getdata(),
dtype=np.uint8).reshape(pil_img.height, pil_img.width, 3)
img = [ [ (int(p[0]),int(p[1]),int(p[2])) for p in
row ] for row in arr ]
return img
def save_img(img, filename):
arr = np.asarray(img, dtype=np.uint8)
pil_img = Image.fromarray(arr)
pil_img.save(filename, format='png')
def create_img(height, width, color):
result = [None] * height
for i in range(len(result)):
result = [color] * width
return result
def height(img):
return len(img)
def width(img):
return len(img[0])
def img_str_to_file(img, filename):
# Converting to integer pixel values
img = np.asarray(img, dtype=np.uint8)
# Calculating max length
max_length = len(str((255,255,255)))
# Limiting the number of rows and columns to be
printed
r_limit = min(20, height(img))
c_limit = min(20, width(img))
with open(filename, 'w') as file:
pix_str = ""
for y in range(r_limit):
for x in
range(c_limit):
# Creating
the string representation
temp_str =
("(" + str(img[y][x][0])
+ "," + str(img[y][x][1])
+ "," + str(img[y][x][2]) +
")")
pix_str +=
temp_str
# Added
appropriate number of spaces to make it visually clear
pix_str +=
" " * (max_length-len(temp_str))
pix_str += "\n"
file.write(pix_str)
return pix_str
functions available in the CSE8AImage library: For this coding
challenge you will be creating an image transformation to turn a
specific part of the image into its negative.
There are 3 files in this section:
CSE8AImage.py - This file SHOULD NOT BE CHANGED. It contains
helper functions that you can use while creating your filter and
writing test cases.
pa6_2.py - This file is where you will define the
negative_region function. To run this file, you should use the
command 'python pa6_1.py' on the terminal
pa6_test.py - This file is where you will do your testing. This
file contains one testing function, and you should add one more
test (instructions below). To run this file, you can use the Run
button.
Function Name: negative_region
Parameter: img - The 2D list representation of an image,
top_left - The (col, row) coordinate of the upper left of the
region to make negative, width - the width of the region to filter,
height - the height of the region to filter
Return: A 2D list representation of the given image with a
negative region starting at top_left.
Description: This filter converts the image into its
photographic negative by returning a new image where the color of
each pixel is computed as follows: Set each component of the color
to be 255 minus the value of the same component in the
corresponding pixel of the original image. Only do this within the
region specified by top_left, width, and height. The region should
begin at the row and column specified by top_left (inclusive). For
example, if a pixel to be changed to negative is (100, 150, 50),
then the resulting pixel should be (155, 105, 205).
Example Result: After defining your function, try running the
test file using the run button or "python pa6_test.py". Note that
if you use the run button, the image will be output to your screen,
but not be saved to your images folder. Writing Test Cases Your
task here is to add ONE test case using an image. This test case
should use one of the other images provided (e.g. Hopper.jpg or
star.png) or you can provide your own image. You should use the
load_img function provided to load your image (take a look at the
first test case to get an idea of how to load an image). To upload
your own image, right click the images directory and select "Upload
here" to upload an image from your computer to Edstem. This test
case should be called test_negative_2(). Don't forget to uncomment
the call to test_negative_2().
pa6_test code:
from CSE8AImage import *
from pa6_2 import *
"""
RGB codes for common colors. You can add on to this
if you want to experiment with different colors.
"""
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
yellow = (255,255,0)
magenta = (255,0,255)
gray = (128,128,128)
purple = (128,0,128)
def test_negative_1():
cat = load_img("images/cat.jpg")
img = negative_region(cat, (110,100), 300, 250)
save_img(img, "images/negative_cat.jpg")
# add test_negative_2:
test_negative_1()
#test_negative_2()
CSE8AImage.py code:
import numpy as np
from PIL import Image
def load_img(filename):
pil_img = Image.open(filename)
arr = np.array(pil_img.getdata(),
dtype=np.uint8).reshape(pil_img.height, pil_img.width, 3)
img = [ [ (int(p[0]),int(p[1]),int(p[2])) for p in
row ] for row in arr ]
return img
def save_img(img, filename):
arr = np.asarray(img, dtype=np.uint8)
pil_img = Image.fromarray(arr)
pil_img.save(filename, format='png')
def create_img(height, width, color):
result = [None] * height
for i in range(len(result)):
result = [color] * width
return result
def height(img):
return len(img)
def width(img):
return len(img[0])
def img_str_to_file(img, filename):
# Converting to integer pixel values
img = np.asarray(img, dtype=np.uint8)
# Calculating max length
max_length = len(str((255,255,255)))
# Limiting the number of rows and columns to be
printed
r_limit = min(20, height(img))
c_limit = min(20, width(img))
with open(filename, 'w') as file:
pix_str = ""
for y in range(r_limit):
for x in
range(c_limit):
# Creating
the string representation
temp_str =
("(" + str(img[y][x][0])
+ "," + str(img[y][x][1])
+ "," + str(img[y][x][2]) +
")")
pix_str +=
temp_str
# Added
appropriate number of spaces to make it visually clear
pix_str +=
" " * (max_length-len(temp_str))
pix_str += "\n"
file.write(pix_str)
return pix_str