def crop(image_array, direction, n_pixels): ##################################### ##########YOUR CODE GOES HERE######## ##################################### #return output_array ''' def rgb_to_grayscale(rgb_image_array): ##################################### ##########YOUR CODE GOES HERE######## ##################################### #return output_array def invert_rgb(image_array): ##################################### ##########YOUR CODE GOES HERE######## ##################################### #return output_array def gaussian_blur(image_array, sigma=0.84089): ##################################### ##########YOUR CODE GOES HERE######## ##################################### #return output_array def image_to_drawing(image_array): ##################################### ##########YOUR CODE GOES HERE######## ##################################### #return output_array ''' if (__name__ == "__main__"): file = 'robot.png' lists = flip_image(utilities.image_to_list(file),1) utilities.write_image(lists, 'new.png')
final.append(row) return final def invert_rgb(image_array): ''' (list) -> (list) Takes in an rgb image array, and returns the inverted image array. ''' m = len(image_array) # len rows n = len(image_array[1][:]) rgim = image_array.copy() for r in range(m): for c in range(n): for i in range(3): rgim[r][c][i] = 255 - rgim[r][c][i] return rgim if (__name__ == "__main__"): file = 'robot.png' utilities.write_image(rgb_to_grayscale( utilities.image_to_list(file)), 'gray.png')
[137, 111, 114], [127, 105, 108], [109, 90, 92], [89, 75, 75], [83, 73, 72], [89, 79, 78], [80, 74, 86], [81, 75, 87], [81, 73, 84], [97, 87, 98], [124, 112, 122], [148, 132, 142], [169, 152, 160], [196, 175, 182], [190, 164, 173], [186, 156, 164], [166, 135, 141], [154, 119, 125], [159, 123, 127], [160, 121, 126], [150, 109, 113], [145, 104, 108], [133, 92, 90], [124, 83, 81], [125, 84, 82], [144, 103, 101], [160, 119, 117]] utilities.write_image(img, 'square.png') utilities.write_image(crop(img, 'down', 7), 'cropped.png') utilities.write_image(expected, 'expected.png')
returns an image that has more evenly distributed pixels throughout the image to allow it to have a better contrast level. This only works with grayscale images examples: >>>histogram_equalization([[1, 1, 0], [0, 0, 0], [0, 0, 1]]) [[170, 170, 0], [0, 0, 0], [0, 0, 170]] ''' cdf = [0] * 256 size1 = len(img_array) size2 = len(img_array[0]) output_array = [] for k in range(0, 256): cdf.append(0) for i in range(size1): output_array.append([]) for j in range(size2): output_array[i].append([]) cdf[img_array[i][j]] += 1 for i in range(size1): for j in range(size2): sum = 0 for k in range(0, img_array[i][j]): sum += cdf[k] output_array[i][j] = sum * 255 / size1 / size2 return output_array if (__name__ == "__main__"): file = 'gray.png' img = utilities.image_to_list(file) img2 = histogram_equalization(img) utilities.write_image(img2, 'equal2.png')
k += 1 new_row.append(new_set) new_set = [] j += 1 k = 0 output_array.append(new_row) new_row = [] i += 1 j = 0 return output_array ''' def gaussian_blur(image_array, sigma=0.84089): ##################################### ##########YOUR CODE GOES HERE######## ##################################### return output_array def image_to_drawing(image_array): ##################################### ##########YOUR CODE GOES HERE######## ##################################### return output_array ''' if (__name__ == "__main__"): file = 'robot.png' utilities.write_image(rotate_90_degrees(utilities.image_to_list(file),1), 'clockwise.png')