Compsci 101, Fall 2017, Lab 8

An APT with sets and nested loops, Working with images

For this lab you'll think about an APT using sets and nested loops to solve it, and investigate processing images and also find hidden images.

The lab

In this lab you will:

There is code to snarf for lab 8, that code is also here.

Getting Credit for Lab 8

To get credit for lab, you will need to do the following by Sunday night.

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.


[Note: ChocolateBar is part of an APT Problem set, so it is due when the APT set is due. ]

Part 1: ChocolateBar

Read the ChocolateBar APT.

  1. Write a function uniqueLetters that returns true if all the letters in its string parameter are different (no repeats) and false otherwise. Use a set for this, you may want to to use list("help") to change a string "help" to the list ["h","e","l","p"], for example.

    Call Return
    str_unique("slight") true
    str_unique("differ") false
    str_unique("monster") true

  2. Discuss with your group how to solve this apt using nested for loops. Give just the two nested for loop lines.
        for 
             for
    

  3. Explain in words how you would solve this problem with nested for loops and what you would iterate over.

For now move on to complete the rest of the lab. If time allows you can come back and finish the APT.


Part 2: Image Processing


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.

In this lab we will explore Images. For the images, we will return a pixel (a tuple of three RGB values).

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, JPG/JPGEG, 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 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 week's lab uses 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.

TinEye is a search engine whose queries are images and whose search results are places that this image can be found. When you get a chance, experiment with it to try to figure out how it works. 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?

Start by snarfing the Lab 8 code from the class website. Alternatively, you can browse code here .

In this lab we will be using a few functions from Python's Image library (shown in the code snippet below). This code opens an image, displays the image, gets all RGB pixel values from the image, sets each pixel's red value to zero, and then displays the image again.

img = Image.open("vases.png") img.show() pixels = [(0,b,g) for (r,g,b) in img.getdata()] img.putdata(pixels) img.show()

The images displayed are shown below, unaltered on the left, no red on the right.

Run the program ImageSample.py which has the code above with the dukelogo image.

NOTE: In some cases there may be a red X by the line "for PIL import Image" and the program will still run. That is ok.

The images above are included in your snarf files. If you accidentally overwrite the original with a transformation, you can re-download the image in the code folder above.

Image Methods

Here's a brief summary of the Image methods we will use in this lab (although PIL provides many more).

In the first part of the lab you'll write several filter functions. Each one takes a single (R,G,B) tuple, changes the values or R, or G, or B (or all of them or none of them) and returns a new (R,G,B) tuple. See the explanation below.

The code for this part of the lab is only in one Python module, ImageProcessing.py.

NOTES1: Changing file names and transformations

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

These two lines:

input_file = "dukelogo.png"
load_and_go(input_file, invert)

The first one you will want to change the image to another image at some point. There are several images with the snarf, including different types of images.

The second line you will need to change the transformation you are testing. The default is "invert" which has already been implemented.

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 processedImage.jpg.

To see the resulting transformation you just need to click on processedImage.jpg 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.

Part 2A: Image Filters

We have started by developing a framework for image filters in Python in the module ImageProcessing. It works by applying the filters you write to each pixel of the image. You should implement each of the following filters: test each one, see if the image looks like the altered blue-devil.

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 10% of their original value. Be careful not to produce a color value outside the range of possible values from 0 .. 255. Don't forget to make the R,G,B values of each tuple an int, e.g., if R is 255, then 255*0.90 = 229.5, but you need that to be an int value like 191 --- just use int(R*0.90). If you cannot tell much difference, then try darkening by 25% instead..
Brighten. Brighten the image by increasing the values of each of the three RGB channels of the pixel by 10% of its 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. If you cannot tell much difference, then instead try 25%.
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).
Posterize. Create a posterized version of the image by reducing its total number of colors. To do this, you should restrict the values each of the three RGB channels of the pixel so that it takes on four distinct values based on a level-range. Specifically, if the value of each R,G,B is between 0 and 63 inclusive, it should be set to 50; if the value is between 64 and 127 inclusive, it should be set to 100; if the value is between 128 and 191 inclusive, it should be set to 150; and if the value is between 192 and 255 inclusive, it should be set to 200. In this way, the 256 possible values each channel can have is resticted to just 4.
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).

Copy and paste your code to these functions in the online form.

Part 2B: Denoise

This image has been shot with a broken camera. The results of the technical inspection concluded that the camera light sensor gave random values for the blue and green channel, while it was diminishing the value of red channel by a factor of roughly 10 times. We would like to "denoise" the picture in such a way it is interpretable by humans by removing the "bad" color channels (blue and green) and enhancing the valid one (red).

  1. Since the blue and green channels have random values, a first step in our denoise process will be to remove the blue and green channels in every pixel of the image. Write the function denoise to return an (R,G,B) tuple in which the R value is unchanged, and the G, and B values are set to zero. Apply this filter to the image noise.png, what does it look like?

  2. Do not worry we still have the red channel to reconstruct our image. Modify denoise so that the red value is multiplied by 10 (and the G, and B values are zero). Run this filter on the images noise.png and iron-puzzle.png. What are the resulting images and from what city do they come from?

  3. Put your code for denoise on the online form.

  4. A second distorted image copper-puzzle.png is provided in your snarf files that is noisy in a different way. Look at it. In this image, the true image is in the blue and green values, however all the blue and green values have all be divided by a number between 10 and 30, so the values are very small. The red values are all just random numbers, noise added on top to obscure things. Write denoise2 to undo these distortions to reveal the true image.

    What is the resulting image? What value did you multiply the G, and B values by to get the "best" image?

  5. Put your code for denoise2 in the online form.

  6. A third image with a hidden message is the image clue2.bmp (check clue.bmp too). Look at both of these images that have hidden messages. To find the hidden message you should change every completely red pixel, e.g., (255,0,0) to black which is all zeros. You should also change any white pixel (all 255 values) to black as well. Write denoise3 to show these changes. When you do this you'll find a hidden message. What is the hidden message for both clue.bmp and clue2.bmp?

  7. Put your code for denoise3 in the online form

Submit

When you are finished, be sure to submit the google form.