In this project you will have the opportunity to programmatically manipulate png image files with python.
First, download project3.zip and unzip it. You should see the following files:
arch.png
, hmcOlinSmall.png
, in.png
, out.png
, spam.png
, trees.png
image_fun.py
: where you will write your project codepng.py
: library to work with png files DO NOT MODIFYpng_helper.py
: helper functions to manipulate png files DO NOT MODIFYRename the folder so that it is project3_uLogin
rather than just project3
. If you are working with a partner, make sure both uLogins are listed.
Next, try running the code: python image_fun.py
. Make sure to run with python, NOT python3. If successful, when you ls
you should see a new file, out2.png
. Open out2.png
and out.png
to make sure they look the same:
open out.png out2.ong
eog out.png out2.png &
invert
Read over the invert
function in image_fun.py
to see how it works. At the bottom of the file, change the call to invert to use the in.png
image: . Once inverted, it should look like: . Similarly:
The invert
function modifies an image to create its negative. That is, all color values are 255 minus their original value. Especially note the use of list comprehension in invert
, which iterates over every pixel in the image and calls change(rgb values)
. It’s easiest to also write greyscale and binarize in the same format: a main function that accepts user input and contains a list comprehension that calls a helper function, change
, on each pixel.
Write a function called grayscale
that modifies an image to make it grayscale. For this, you’ll want to do something similar to invert
, except that your new change function will calculate the luminance of the pixel. For example:
Since luminance is an indication of how white/black a pixel is, having your helper function return a list of RGB values in greyscale is easy – just return the same value in each of the three color channels! How much gray? You can use the following equation for luminance:
Gray = 0.2126 * Red + 0.7152 * Green + 0.0722 * Blue
Hints:
Write a function called binarize
, which binarizes an image (makes it black and white) with a threshold thresh
given by the user as a parameter. This threshold is a brightness value between 0 and 255 - if a pixel is greater than the threshold value, then it should turn white, and if its less than the threshold value, then it should turn black. So, a threshold value of 0 means that your photo will turn white and a threshold of 255 means your photo will turn black. For example:
Write a function called flipVert
, which flips the image on its horizontal axis (the bottom is on the top and the top is on the bottom). For example:
You will use the same basic structure as the earlier problems–one main function that opens the file and calls the helper function in a list comprehension.
Hints:
flipVert
, you’ll want to iterate over only the rows instead of the pixels in Im_pix
and reverse their order.L
is a list, then L[::-1]
is the reverse of that list.Write a function flipHoriz
that flips the image on its vertical axis. This should work in the same way as flipVert
but flip in the horizontal direction. For example:
Instead of reordering the rows, you want to consider how the pixels in the rows reorder when an image is flipped horizontally. Note: flipping in.png
horizontally has no effect because it is symmetric about the vertical axis.
Write a function mirrorVert
that mirrors the image across its horizontal axis (i.e., so that the top part is mirrored upside down on the bottom of the image). The easiest approach is to replace the bottom half of Im_pix
with the reversed rows from the top half of flipVert
. To do this, you’ll use the built-in function getWH()
in png_helper.py
to get the height of Im_pix
. Keep in mind that getWH()
returns a list of width & height. You can assign both values to variables with something like width, height = getWH()
.
Warning: You cannot create a copy of a list with list1 = list2
, because instead of creating a new object, Python will just create a new reference to the old list. However, list slicing does create new copies of data, so consider how you could combine two slices, each of half the size of the original list, in order to mirror the image…
Write a function mirrorHoriz
that mirrors the image across its vertical axis. This should work similar to mirrorVert
, but instead of replacing the bottom rows with the reversed top rows (as you did in mirrorVert
), you’ll replace the last half of the pixels in every row with the reversed first half of the pixels. For example:
(The bigger question: which side of the SPAM can do you open?)
Write a function scale that
scales the image to half of each of its original dimensions (this will be a quarter of its original area). The easiest way to do this is to eliminate every other pixel in each row (scaling the image horizontally) and eliminate every other row (scaling the image vertically).
Make your own transformations! Make sure to comment.
Make sure your image_fun.py
file contains 7 additional functions: grayscale
, binarize
, flipVert
, flipHorz
, mirrorVert
, mirrorHoriz
, and scale
. Be sure to test them all at the bottom of the file. Please make your output filenames are consistent. For example, if your input image file is x.png
, name your output x_out.png
. Please remove your output files before submitting.
Make sure you’ve added your name to the comments at the top of the file, that your program contains comments and follows good programming style.
Once you’re satisfied that your programs are working correctly, zip it for submission:
cd ..
zip project3_uLogin.zip project3_uLogin/*
Assignment adapted from Harvey Mudd’s CS 5 Fun with Images.