Week 3, Problem 3: Looks
Good!
[30
points; individual or pair]: hw3pr3.py
See special submission
instructions at the bottom of this page
In this problem you will get more practice working with
2D lists, this time to manipulate images.
Download the file mimp.pyw
from this link and save it wherever is
convenient for you. This file is based on the file linked from http://sandbox.mc.edu/friendly_python/lab4.html
and provides basic image manipulation capabilities (plus the ability to add
much more).
Next create a new folder on your desktop called
"modifiers" (without the quotes). Into that directory, download the
file modifyBase.py
from
this link.
NOTE FOR MAC/LINUX USERS:
The program previously was not finding the modifiers directory on the desktop
on Macs of Linux machines. This should be fixed now for Macs (but you have to
re-download the mimp.py file). In Linux it will look in your home directory.
However you can always set the location of the modifiers directory manually in
the application by going to the Modifiers menu and choosing
"Location...".
For the rest of this problem, you will run your code only by running the mimp.py
file.
You can run this file in two ways. First, you should be able to double-click on
it and it should open a window that allows you to open up images.
Alternatively, you can open the file in IDLE and then press F5. Try this now.
You'll also need some image files to work with. There
are several that you can grab here: http://sandbox.mc.edu/friendly_python/lab4.html,
but feel free to use whatever images you like (but you'll want to keep them
small, and as they get bigger you'll need to increase your recursion depth as
you've done in previous homeworks).
Run the mimp.py
file and try opening some
image files now.
Once you have things set up, you're ready to start
writing code that modifies images. We'll step through an example together so
you get the hang of the general process and then you'll write several more
image manipulation functions of your own.
brighten.py
and save it in the modifiers directory
you created above brighten.py
from modifyBase import *
label = "Brighten"
ordinal = 1
def modify(pic):
""" modify modifies an image
to make it brighter """
pixels = getPixels(pic)
if len(pixels) == 0:
return
newPixels = [ [setNewPixel( pixels, row, col
) for col in range(len(pixels[0]))] for row in range(len(pixels))]
setPixels(pic, newPixels)
def setNewPixel( pixels, row, col ):
""" setNewPixel returns the
NEW imanges (row, col) (r,g,b) value
input pixels: a 2D list containing RGB
information in the pixels in a picture
input row: the row of the pixel in question
input col: the column of the pixel in
question
"""
rval= min(pixels[row][col][0]+30, 255)
gval = min(pixels[row][col][1]+30, 255)
bval = min(pixels[row][col][2]+30, 255)
return (rval, gval, bval)
mimp.py
file again. You should see a button at the bottom of the window named
"brighten". Load an image and click on this button. You should
see the image brighten each time you click on the button. Looking at the above code, we see that it functions in
a similar way to the Lights On code from lab. modify
gets the pixels out of an image, creates a new 2D list containing the new color
values for the pixels, and then sets the image to have these new color values. setNewPixel
does the work of determining a new color for each pixel.
Representing
Color
As we mentioned in class, images on the computer are
represented as a grid of pixels, where each pixel has a red value, a green
value and a blue value, which together determine the color of that pixel (i.e.,
its RGB value).
Each pixel gets one byte for each color channel, so each color value can range
from 0 to 255 (2^8-1).
The function getPixels
above returns a 2D list
of tuples containing RGB values. E.g., after the following code is executed
pixles = getPixels(pic)
(r,g,b) = pixels[row][col]
(r,g,b)
will hold the red, green and blue values for the pixel at row row
,
column col
in the image.
Remember that tuples are essentially the same as lists,
with some subtle differences that we'll address later in the semester. You can
still index into them and slice them, and append them, just like you did with
lists. E.g.,
>>> t = (1, 2, 3)
>>> t[0:1]
(1,)
>>> t[1]
2
>>> t[0:2]
(1, 2)
>>>
For now the only difference is that tuples are created
(and displayed) with parentheses. Notice that a tuple with only one element is
displayed with a comma after that element.
Functions
for you to write
Your task is to implement (at least) 3 of the following
functions, one from each group. For up to +15 points of extra credit (depending
on the scope of your additions) you can implement more of them or make up your
own creative image manipulation function.
For each function you write, create a new .py
file in your modifiers directory. You'll paste them all together when you
submit. I recommend always starting with the brighten.py
code by choosing the "Save As..." option in the File menu and then
modifying the brighten code to suit your needs. The function modify
should remain unchanged. You will change the setNewPixel
function.
Group 1:
negative.py
:
Modifies an image to create its negative. That is, all color values should
be 255 minus their original value grayscale.py
:
Modifies an image to make it grayscale (i.e. black and white). Group 2:
flipVert.py
:
Flip the image on its horizontal axis flipHoriz.py
:
Flip the image on its vertical axis mirrorVert.py
:
Mirror the photo across its horizontal axis (i.e., so that the top part is
mirrored upside down on the bottom of the image mirrorHoriz.py
:
Same as above, but across the vertical axis Group3:
scale.py
:
Scale the image to half its original size (either horizontally,
vertically, or both) blur.py
:
Blur the image by combining neighboring pixels in some way (up to you) randomGrid.py
:
Divide the image into an NxN grid (where the N is up to you) and randomly
sort the pieces of the grid across the image. Feel free to add any more functions you can think of.
Be creative! I'll show off the more interesting images in class.
Submitting
your files
You have created several files for this problem, but
you'll need to combine them into one file for submission. Create a new file
named hw3pr3.py
.
Into this file copy and paste all of your image manipulation files. Be sure to
include a comment between each, like this:
########
Code for grayscale.py starts here ##########
And at the top of your file you should include a
comment listing the functions you implemented.
Submission
You
should submit your hw3pr3.py
file at Canvas .