In: Computer Science
Part 1: Image effects
Your first task is to write two functions that manipulate digital images.
You will write two functions that take an image object, create a copy, perform a pixel-by-pixel manipulation, and return the manipulated copy. We have provided three helper functions (open_image, display_image, and save_image) that you can use to test your code. You may write any additional helper functions that you find useful. Below there are descriptions and sample images for each of the required manipulations.
The best way to test your image functions is to modify the main function at the bottom of hw7_images.py. You can add calls to the various image functions and use the helper functions to display and save the new/modified image.
The code for an example function, red_filter, is provided in hw7_images.py; images to show the effect of this filter are shown below.
The two functions you must write are as follows:
1. negative
Create a negative of the image by inverting all of the color values. Recall that the minimum color value is 0 and the maximum is 255. So a color value of 255 becomes 0 and a value of 33 becomes 222.
SOLUTION
This is the required function:
def negative(image_obj):
#copying the image
img_copy = image_obj.copy()
h,w,_ = img_copy.shape
for i in range(h):
for j in range(w):
pixel_value = list(img_copy[i,j])
#negating all chanel values
pixel_value[0] = abs(255-pixel_value[0])
pixel_value[1] = abs(255-pixel_value[1])
pixel_value[2] = abs(255-pixel_value[2])
img_copy[i,j] = pixel_value
return img_copy
________________________________________________________________________________
here is the total code and the sample output:
Left side is the inverted image and the right side is the actual image
Comment if any doubts