Computer Science 110
Foundations of Computing through Digital Media

Denison

CS 110 Lab Project 6

Loops and Pictures

Due: By 5 p.m. Thursday, October 11th

In this lab, after starting with some loops to practice our sequences once again, and then a couple printing loop exercises, you will practice with loops for problem solving with JPEG pictures.

Problem 1: Sequences

Recall the four sequences from the last lab:

1. 5, 6, 7, 8, 9, 10,...

2. 0, 3, 6, 9, 12, 15,...

3. 0, 1, 4, 9, 16, 25,...

4. 0, 1, 3, 7, 15, 31,...

In a file called sequences.py, create a main() function that calls 4 subordinate functions, one for each of your sequences. You may name these functions anything you like, but they should each take a parameter, i. Each function should use a while loop to generate their given sequence from the first up to and including the ith element in the sequence, printing one number in the sequence per line. The main() function should request i from the user before calling any of the sequence functions.

Problem 2: Nested Loop Printing Exercises

Described below are patterns we want to generate (and print) with nested loops. Create a function for each, and create a main() function in a file called patterns.py, wherein the main() calls the functions for each of the patterns.

5. Write a loop that prints the first ten numbers on one line (with spaces in between), and then repeats this 5 times, so that we have 5 rows of the numbers 1 through 10.

6. Write a loop that prints the following:

1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5

7. Write a loop that prints the following:

1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
5 4 3 2 1

8. Write a loop that prints the following:

1
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

Problem 3: Image Manipulation

In the remaining problems, we will use the JES enviroment for creating Picture objects and for manipulating those Picture objects to achieve the described effects.

If you look in the course shared directory under Resources, you'll find a folder called media.  This contains a number of images.  In this part, you will be writing three programs.  The first will invert the colors of an image.  The second will create the grayscale of an image.  And the last will posterize an image.  For example, if your initial image was something like

then the inverted, grayscale, and posterized versions will look respectively something like this:

You can use whatever images you want from that folder, just pick something you like. For each of the following problems, the main() function will perform a pickAFile() and then main() will create and show the Picture object. the main() function will then call a subordinate function that takes a picture object as a parameter and performs the transformation described below, finishing with a call to repaint(). After completion of the transformation, the main program should ask the user if they wish to save the file and, if so, save a copy of the transformed picture to a location of the user's choice.

When you turn in your assignment, I would like a jpeg of your favorite results for each of the three transformations.

Invert.py

Write a program that inverts the color of each pixel in a given image and display the image to the screen.  An inverted image has each channel of each pixel "inverted".  That is, if the green value was 0, it is now 255.  If the green value was 1, it is now 254, and so on.

Grey.py

Write a program that makes a greyscale version of an image.  Recall that shades of grey are created by setting all three color channels equal.  Of course, picking arbitrary shades of grey for each pixel won't produce much of anything.  You'll want to set the grey value to match the average brightness of the pixels.  For example, if the pixel had a color given by (50, 50, 200) then the greyscale version should be (100, 100, 100), since (50 + 50 + 200)/3 = 100. 

Poster.py

Write a program to posterize an image.  Posterizing is done by reducing the number of colors the image uses in a natural way.  For example, currently the blue channel of any given pixel could have any integer value from 0 to 255.  To posterize the image, we might simplify things by only allowing 3 values of blue: perhaps if the blue is between 0 and 100, we set it to 50, if between 101 and 200 we set it to 150, and if between 201 and 255 we set it to 250.  If we were to do the same with red and green, then each color channel could only have 3 possible values, and thus the total number of possible colors would be 3 x 3 x 3 = 27.  This is in stark contrast to the millions of colors that were originally possible.  This creates a posterized effect (the way old posters looked when there were only a few colors that could be printed at any one time). 

Play around with how many colors you allow of any particular channel, how you vary the ranges of colors, and how you pick what each range gets mapped to.  Find something you like.  The most natural way to set colors would be with if or if-else statements, although you could also use modular arithmetic if you're just planning on having regular intervals of color.