USING PYTON
In this question, you will write some functions to manipulate an image in different ways. Note that you can and should use NumPy arrays, functions and methods for this question. Further, please use the provided image_tester.py file to test your functions. 1. [15 points) Write a function image-to-greyscale that takes in two arguments: a string image filename, and a tuple weighting containing three floats. The function should re- turn a NumPy array representing a greyscale version of the image, that is, an image with only shades of grey. You will have to load the image and perform the greyscale conversion. Note that you are not allowed to use any third-party function for the greyscaling (e.g., you cannot use the rgb2grey function as seen in class). Instead, you should transform each pixel element in the image array from an array of three numbers (red, green and blue color intensities) into a single number representing the shade of grey. Use the following formula to convert each pixel: pirel = r.picel, +9* pixel, + b* pirely where r, g, b are the three elements of the weighting tuple passed in as argument, and pirel, pi.relo, pirely are the RGB color intensities of the pixel. 2. [25 points) Write a function combine.images() that reads two images from files, selects a rectangular region from each image, and places those two regions side-by-side (horizontally) to produce a new image array. The function takes six arguments: file1, file2, toplefti, topleft2, height and width. The arguments files and file2 are strings containing the names of the image files to be read into memory as NumPy arrays. The argument toplefti will be a list of two integers [top, left] indicating the top-left corner of the rectangular region for the first image (the one in filei). Similarly, the list topleft2 will indicate the top-left corner of the rectangular region for the second image (the one in file2). For the sake of simplicity, the rectangular regions for both images will have the same size, as specified by the arguments height and width. Thus, given the two rectangular regions, one for each image, the function should then combine both regions side-by-side (horizontally) by selecting all pixels in both regions and putting them into a new NumPy array. The return value of the function should be this new NumPy array with the horizontally combined regions. You can assume that the lists topleft1 and topleft2 will contain valid array indices, i.e., the locations of the top-left corners will be within the image boundaries. However, the height and width arguments could be such that the region goes beyond the image bounds. In such a case, as shown in Example 4, pixels outside the image should be considered as black (RGB value of [0, 0, 0]). from image import image_to_greyscale, combine_images import matplotlib.pyplot as plt #Example 1 greyscale = image_to_greyscale(“flower.jpg”, (0.2125, 0.7154, 0.0721)) plt.imshow(greyscale) plt.show() #Example 2 image = combine_images (“monkey.jpg”, “tiger.jpg”, [600, 900], [50, 550], 400, 400) plt.imshow(image) plt.show() #Example 3 image = combine_images (“tiger.jpg”, “monkey.jpg”, [50, 550], [600, 900], 600, 400) plt.imshow(image) plt.show() #Example 4 image = combine_images(“monkey.jpg”, “tiger.jpg”, [600, 900], [450, 1100], 400, 400) plt.imshow(image) plt.show() Show transcribed image text In this question, you will write some functions to manipulate an image in different ways. Note that you can and should use NumPy arrays, functions and methods for this question. Further, please use the provided image_tester.py file to test your functions. 1. [15 points) Write a function image-to-greyscale that takes in two arguments: a string image filename, and a tuple weighting containing three floats. The function should re- turn a NumPy array representing a greyscale version of the image, that is, an image with only shades of grey. You will have to load the image and perform the greyscale conversion. Note that you are not allowed to use any third-party function for the greyscaling (e.g., you cannot use the rgb2grey function as seen in class). Instead, you should transform each pixel element in the image array from an array of three numbers (red, green and blue color intensities) into a single number representing the shade of grey. Use the following formula to convert each pixel: pirel = r.picel, +9* pixel, + b* pirely where r, g, b are the three elements of the weighting tuple passed in as argument, and pirel, pi.relo, pirely are the RGB color intensities of the pixel. 2. [25 points) Write a function combine.images() that reads two images from files, selects a rectangular region from each image, and places those two regions side-by-side (horizontally) to produce a new image array. The function takes six arguments: file1, file2, toplefti, topleft2, height and width. The arguments files and file2 are strings containing the names of the image files to be read into memory as NumPy arrays. The argument toplefti will be a list of two integers [top, left] indicating the top-left corner of the rectangular region for the first image (the one in filei). Similarly, the list topleft2 will indicate the top-left corner of the rectangular region for the second image (the one in file2). For the sake of simplicity, the rectangular regions for both images will have the same size, as specified by the arguments height and width. Thus, given the two rectangular regions, one for each image, the function should then combine both regions side-by-side (horizontally) by selecting all pixels in both regions and putting them into a new NumPy array. The return value of the function should be this new NumPy array with the horizontally combined regions. You can assume that the lists topleft1 and topleft2 will contain valid array indices, i.e., the locations of the top-left corners will be within the image boundaries. However, the height and width arguments could be such that the region goes beyond the image bounds. In such a case, as shown in Example 4, pixels outside the image should be considered as black (RGB value of [0, 0, 0]).
from image import image_to_greyscale, combine_images import matplotlib.pyplot as plt #Example 1 greyscale = image_to_greyscale(“flower.jpg”, (0.2125, 0.7154, 0.0721)) plt.imshow(greyscale) plt.show() #Example 2 image = combine_images (“monkey.jpg”, “tiger.jpg”, [600, 900], [50, 550], 400, 400) plt.imshow(image) plt.show() #Example 3 image = combine_images (“tiger.jpg”, “monkey.jpg”, [50, 550], [600, 900], 600, 400) plt.imshow(image) plt.show() #Example 4 image = combine_images(“monkey.jpg”, “tiger.jpg”, [600, 900], [450, 1100], 400, 400) plt.imshow(image) plt.show()
Expert Answer
Answer to In this question, you will write some functions to manipulate an image in different ways. Note that you can and should u…