Compsci 101, Fall 2014, Lab 6

The lab

In this lab you will:

Snarf the code for lab 6 before getting started. The files for lab6 Part 2 are also here,

Getting Credit for Lab 6

To get credit for this lab, you will need to enter your names and netids, and the answers to several questions on an online form.

Complete this form for credit for lab 6

(the same questions are accessible here as well).

Part 1: List Comprehensions

A Python list comprehension allows you to create a list from another list/sequence. For example the list comprehensions below generate the output shown. Note that a list comprehension is a list defined by a for-loop inside square brackets, and that the loop iterates over some list, creating a new list in the process. In the examples below, the output of each print statement is shown in italics.

print [x*2 for x in range(0,10)]
[0,2,4,6,8,10,12,14,16,18]



print [s[0] for s in ['apple', 'termite', 'elephant']]
['a','t','e']



print [n % 2 == 0 for n in [2,4,6,1,3,5,8]]
[True,True,True,False,False,False,True]


print [n for n in [2,4,6,1,3,5,8] if n % 2 == 0]
[2,4,6,8]


Without using a computer, (so by thinking) show what each line below prints. Enter your answers in the online form.
  1. print [x**2 for x in range(1,10,2)]
    
  2. print [s[1] for s in ["big", "brown", "bare", "stem", "pea"]]
    
  3. print ['po'*i for i in range(0,5)]
    
  4. print [i*i for i in range(1,10) if i % 2 == 0]
    
  5. print sum([1 for p in [1,2,3,4,5,6,7,8]])
    
  6. print sum([1 for x in "ostentatiously" if "aeiou".find(x) > -1])
    

    Generate List Comprehensions

  7. Using a list comprehension write one expression using the function sum that returns the sum of all the odd integers in a list of integers named nums -- so for [1,3,2,4,5] your expression should evaluate to 9 --- fill in the parentheses: sum(...).

  8. Using a list comprehension and the list functions sum and len write an expression that returns the average length of all the strings in a list of strings named strs. So you'll call two list functions: one with strs as an argument and one with a list comprehension using strs as an argument: combining in one expression sum(...) and len(...). For example, the average length of the strings in the list below is (3+3+4+5)/4 = 15/4 = 3.75
      strs = ["cat", "dog", "bear", "tiger"]
    


Part 2: Transforming Images

Images are a powerful way of conveying information and computers give us powerful way of modifying images. This lab involves manipulating pixmaps by transforming each color in the original pixmap using the same algorithm (such as darkening, inverting, posterizing, coverting it to grey scale). Your program will then perform several operations on these images to acheive a larger effect.

Images are stored by computers in a variety of formats, such as GIF, JPEG, TIFF, and PNG. These formats differ in how faithfully they represent the original picture, how well they can be compressed to reduce the space each image takes up, or how well they can be copied from one type of computer to another. However, no matter what format the image is stored in, it can always be represented of as a mapping of (x, y) pixel position to a color (the range of colors may be restricted to values of grey or just black and white). Thus, for the remainder of this project, we will refer to all formats of digital images as images.

This lab introduces you to the Python Imaging Library (PIL) which provides a simple interface for processing images. We will create basic color filters (RGB color model) that can be applied to images then combine them to create larger effects.

Can the search results be different file formats than your query (e.g., JPEG vs. GIF)? How much detail/quality can you leave out of the image without throwing off the results?


The most famous professional-grade image modification program is Photoshop, and images are often "photoshopped" to enhance certain properties. This photo was released by the Iranian government in 2008 to display the power of their new missiles, and they photoshopped it so that the missile launch appeared larger.

Image courtesy of Little Green Footballs.

Start by snarfing the lab code from the course website (alternatively, you can browse code here):

http://www.cs.duke.edu/courses/compsci101/fall14/snarf/

This code for this lab is divided into two Python modules, ImageUtils and Transforms.

NOTES1: Changing file names and transformations

When transform.py runs, there are two lines at the bottom of the file you will want to change to see different results.

These two lines:

inputFile = "duke_logo.png"
image = ImageUtils.showTransformedImage(inputFile, identity)

The first one you will want to change the image to another image at some point. Try the other image, vases.png.

The second line you will need to change the transformation you are testing. The default is "identity" which doesn't change anything.

NOTE2: For those on a PC with Windows 7 OR IF YOU GET ANY STRANGE ERRORS WHEN RUNNING THIS PROGRAM

If you are on a PC with Windows 7 (and possibly others might also have this problem), you will see errors when it tries to display the image through the program. That's ok, the transformation will modify the file transform_results.png.

To see the resulting transformation you just need to click on transform_results.png to see the change. You may have to refresh the project folder in Eclipse the first time you run the program to see this file.

Transforming Colors

You should implement the following transforms:

GreyScale. Create an image that has only shades of grey values by computing the average value of the pixel's three RGB channels and using that average value for all three channels of the new color. For example, if the pixel has the values (255, 0, 128) for its red, green, and blue channels, respectively; then the resulting color should have the values (127, 127, 127).
Invert. Create a photographic negative of the image by inverting each of the three RGB channels of the pixel. For example, if the pixel has the values (255, 0, 128) for its red, green, and blue channels, respectively; then the resulting color should have the values (0, 255, 127). In other words, each value is the other's opposite within the range of possible values from 0 .. 255. Thus, if this operation is performed on an image twice in succession, the image would appear unchanged.
Darken. Darken the image by reducing the values of each of the three RGB channels of the pixel by 25% of their original value. Note, this method should exactly undo the results of performing the Brighten filter (as long as none of the image's colors are already as bright as possible), so that doing one operation then the other should result in no net change in the image. Be careful not to produce a color value outside the range of possible values from 0 .. 255.
Brighten. Brighten the image by increasing the values of each of the three RGB channels of the pixel by 25% of their original value. Note, this method should exactly undo the results of performing the Darken filter (as long as none of the image's colors are already as dark as possible), so that doing one operation then the other should result in no net change in the image. Be careful not to produce a color value outside the range of possible values from 0 .. 255.
Solarize. Create a solarized version of the image by inverting the values of each of the three RGB channels only if they are too dark, i.e., less that 128. To do this, you should check the value of each channel and only if it is less than 128, invert its value (i.e, subtract it from 255). For example, if the pixel has the values (255, 0, 128) for its red, green, and blue channels, respectively; then the resulting color should have the values (255, 255, 128).

Once you get the program working you should change the name of the image file to vases.png

Cut and paste your code for each transformation into the online google form.