title: Lab 7: Loops; working with image objects [TOC] ### Task 0: trace nested `for` loops 1. Trace the following `for` loop using a table, and determine the output: :::python for i in [0, 1, 2]: for j in [3, 5]: k = i + j print(k) print(i) Here's the beginning of the table: | `i` | `j` | `k` | value printed | |-----|-----|-----|:-------------:| | `0` | `3` | | | | | `5` | | | | ... | | | | | | | | | Remember that the inner loop executes in its entirety for each value of the outer loop. 2. Trace the following `for` loop using a table, and determine the output: :::python for i in [3, 2, 1]: for j in range(i): print(i * j) print(j) Here's the beginning of the table: | `i` | `range(i)` | `j` | value printed | |-----|-------------|-----|:-------------:| | `3` | `[0, 1, 2]` | `0` | | | ... | | | | Because the sequence used by the inner loop (`range(i)`) depends on the value of the outer loop's variable (`i`), we've included a column in the table for that sequence. ***At the end of the lab, you should turn in your paper trace to the TF. To get credit for attendance at today's lab, you must have made a good effort on this trace.*** ### Task 1: understand and use image objects **Pixels and RGB values** Digital images are composed of many small *pixels*, and one way to capture the color of a single pixel is using a list of the form [red, green, blue] where `red`, `green`, and `blue` are integers between 0 and 255 that represent the amount of red, green, and blue that is present in that pixel. These numbers are known as RGB values. For example: | color | [R, G, B] values | |-----------|------------------| | pure red |`[255, 0, 0]` | | pure blue |`[0, 0, 255]` | * ***What list of integers would represent a pure green pixel?*** Colors other than red, green, and blue are formed using combinations of red, green, and blue. Two special cases worth mentioning are: | color | [R, G, B] values | |--------------|------------------| | pure white |`[255, 255, 255]` | | pure black |`[0, 0, 0]` | **Using image objects** In [Problem Set 7][ps7], you will use an image-processing module that allows you to manipulate the pixels in images stored in [PNG format][png]. In the rest of the lab, we will practice using this module. 1. Begin by downloading the following zip file: [lab7.zip](../files/labs/lab7/lab7.zip) Unzip this archive, and you should find a folder named `lab7`, and within it several files, including `lab7.py`. **You should *not* move any of the files out of the `lab7` folder.** Keeping everything together will ensure that your functions are able to make use of the image-processing module that we've provided. 2. Open `lab7.py` in IDLE. Near the top of the file you should see the following statement: :::python from cs111png import * This line imports the image-processing module that you will be using in this lab and in [Problem Set 7][ps7]. 3. Run `lab7.py` so that you will have access to the code in this module. 4. Among the files in the `lab7` folder are the following PNG images: `rhett.png`:

![rhett.png][rhett] `bone.png`:

![bone.png][bone] To manipulate these images from the Shell, we start by doing the following: >>> rhett = load_image('rhett.png') >>> bone = load_image('bone.png') After these lines execute, the variable `rhett` represents the image `rhett.png`, and the variable `bone` represents the image `bone.png`. More precisely, `rhett` and `bone` are image *objects*. 6. Once we create an image object using `load_image`, we can use that object to manipulate the underlying image. To do so, we use one of the image object's *methods* -- i.e., one of the functions that is inside the object. For example: >>> rhett.get_width() 274 >>> bone.get_width() 100 These calls tell us that the image `rhett.png` is 274 pixels wide and the image `bone.png` is 100 pixels wide. >>> rhett.get_pixel(75, 70) [255, 0, 0] This call tells us that the pixel at row 75, column 70 of `rhett.png` has RGB values of `[255, 0, 0]`, which means that it is pure red. >>> bone.get_pixel(10, 80) [0, 0, 0] This call tells us that the pixel at row 10, column 80 of `bone.png` has RGB values of `[0, 0, 0]`, which means that it is pure black. 6. Here's a list of the key methods that are inside every image object: +------------------------------------------+---------------------------+ | method name | what it does | +==========================================+===========================+ | `img.get_width()` | returns the width of the | | | image represented by the | | | image object `img` | +------------------------------------------+---------------------------+ | `img.get_height()` | returns the height of the | | | image represented by the | | | image object `img` | +------------------------------------------+---------------------------+ | `img.get_pixel(r,` `c)` | returns the list of RBG | | | values for the pixel at | | | row `r` and column `c` | | | of the image represented | | | by the image object `img` | +------------------------------------------+---------------------------+ | `img.set_pixel(r,` `c,` `rgb)` | changes the RBG values | | | values for the pixel at | | | row `r` and column `c` | | | of the image represented | | | by the image object `img` | | | to the list of RBG values | | | given by `rgb` | +------------------------------------------+---------------------------+ | `img.save(filename)` | saves the image | | | represented | | | by the image object `img` | | | in a file with the | | | specified `filename` | +------------------------------------------+---------------------------+ Given the image objects `rhett` and `bone` that we created above, solve the following problems, and test your answers in the Shell: 1. Write a method call to determine the height of the image `bone.png`.
(*you should get* `100`) 2. Write a method call to get the RGB values for the pixel at row 310, column 135 of the image `rhett.png`.
(*you should get* `[255, 0, 0]`*; if you get an error saying that the row and/or the column are out of bounds, make sure that you're using the correct image object, and that you are specifying the row and column in the correct order*) 3. Write a method call to get the RGB values for the pixel in the lower right-hand corner of `rhett.png`.
(*you should get* `[255, 255, 255]`) * start by determining both the height `h` and width `w` of the image * use the dimensions to determine the row and column coordinates of the lower right-hand corner; the following image may help:

![pixel_grid.png][pixel_grid] 4. Write a method call to change the RGB values for the pixel at row 10, column 80 of `bone.png` to pure green (using RGB values of `[0, 255, 0]`).
(*you won't see any return value for this method call*) 5. Write a method call to change the RGB values for the pixel at row 120, column 200 of `rhett.png` to pure blue.