Question
Problem Set 7 included a problem in which you manipulated PNG images using nested loops. In those problems, you used image objects and their associated
Problem Set 7 included a problem in which you manipulated PNG images using nested loops. In those problems, you used image objects and their associated methods (e.g., get_pixel) to access and change the underlying pixels of the image. If you were to look inside one of those image objects, you would see that the pixels are actually stored in a two-dimensional list, and that the methods of the image object give you indirect access to that 2-D list. In this problem, we will bypass the use of image objects and work directly with 2-D lists!
Getting started Begin by downloading the following zip file: ps8image.zip
Unzip this archive, and you should find a folder named ps8image, and within it several files, including ps8pr4.py. Open that file in IDLE, and put your work for this problem in that file. You should not move any of the files out of the ps8image folder. Keeping everything together will ensure that your functions are able to make use of the image-processing module that weve provided.
Loading and saving pixels At the top of ps8pr4.py, weve included an import statement for the hmcpng module, which was developed at Harvey Mudd College. This module gives you access to the following two functions that you will need for this problem:
-load_pixels(filename), which takes as input a string filename that specifies the name of a PNG image file, and that returns a 2-D list containing the pixels for that image
-save_pixels(pixels, filename), which takes a 2-D list pixels containing the pixels for an image and saves the corresponding PNG image to disk using the specified filename (a string).
In addition, weve given you a function create_uniform_image(height, width, pixel) that creates and returns a 2-D list of pixels with height rows and width columns in which all of the pixels have the RGB values given by pixel. Because all of the pixels will have the same color values, the resulting grid of pixels will produce an image with a uniform color i.e., a solid block of that color.
We demonstrated the use of all three of these functions in Lab 8. We also provided examples of manipulating images represented as 2-D lists. We encourage you to review that material from lab before proceeding.
Your tasks
1.Write a function blank_image(height, width) that creates and returns a 2-D list of pixels with height rows and width columns in which all of the pixels are green (i.e., have RGB values [0,255,0]). This function will be very easy to write if you take advantage of the create_uniform_image function that weve provided!
2.Write a function flip_vert(pixels) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list of pixels for an image in which the original image is flipped vertically. In other words, the top of the original image should now be on the bottom, and the bottom should now be on the top. For example, if you do the following:
This function will be similar to the mirror_vert function that you wrote in Problem Set 7, but it will flip the image vertically instead of mirroring it. In addition, there are several other key differences:
3.Write a function reduce(pixels) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list that represents an image that is half the size of the original image. It should do so by eliminating every other pixel in each row (to reduce the image horizontally) and by eliminating every other row (to reduce the image vertically).
There are different approaches that you can take to this problem, and there are two possible results that you can obtain when you reduce a given image. For example, if you do the following:
4.
Write a function transpose(pixels) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list that is the transpose of pixels.
You should start by copy-and-pasting your transpose function from Problem 3 into ps8pr4.py. The only real change that you need to make to your earlier function is to change the helper function that gets called to create the new 2-D list. Make this version oftranspose call your blank_image function to create the new 2-D list. We also recommend changing the name of the variable matrix to pixels, but doing so is not essential.
Heres one way to test your new version of transpose:
5. Write two more functions, both of which should take a 2-D list pixels containing pixels for an image, and create and return a new 2-D list of pixels for an image that is a rotation of the original image:
- rotate_clockwise(pixels) should rotate the original image clockwise by 90 degrees. For example:
- rotate_counterclockwise(pixels) should rotate the original image counterclockwise by 90 degrees. For example:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started