1.1. Complement
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 an image into its complement. The complement
of an image can be achieved by swapping its red and blue values.
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_1.py - This file is where you will define the complement
function. To run this file, you should use the command 'python
pa6_1.py' on the terminal. This might be helpful while debugging
your code.
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: complement
Parameter: img - The 2D list
representation of an image
Return: A 2D list representation of complement
image of the given image
Description: This filter returns a new image
where the blue and red color components of every pixel are swapped.
This filter is not standard as the resulting image does not have
much meaning, but can sometimes look interesting as in this
example. For example, if a pixel in the image is (100, 150, 50),
the resulting pixel would be (50, 150, 100).
Example Result:
Before:
After:
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
You are required to add at least one test case to pa6_test.py.
This test case should be a function called test_complement_2(). You
should use the create_img function to create an image, apply the
function, and save the result as an image. Don't forget to
uncomment the call to test_complement_2() at the end.
The create_img function can be used as follow to create a red
20x20 image:
img = create_img(20, 20, (255, 0, 0))
Private Test Cases
This problem has 3 private test cases. You will not be able to
see if you pass these tests when you click mark - so make sure you
test these cases on your own
An image with a single pixel
An image with a single row of 5 pixels
An image with 6 rows and 5 columns
from CSE8AImage import *
# Define your complement function here:
from CSE8AImage import *
from pa6_1 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_complement_1():
cat = load_img("images/cat.jpg")
img = complement(cat)
save_img(img, "images/complement_cat.jpg")
#define test_complement_2() here:
test_complement_1()
# DO NOT EDIT THIS FILE
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
test_complement_2()
please use python
1.1. Complement Here is the documentation of all of the functions available in the CSE8AImage library: For this coding c
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am