15110 Summer Session Two - 2014

Lab 12 - Thursday, July 31 [Your Last Lab!]

CA Activities

Demonstrate loading and displaying the nautical flags image.

Files

Deliverables

  1. flip_image.py
  2. c_flag.py
  3. swap_bg.py
  4. grayscale.py
  5. transpose.py

Goals

In previous labs you've seen how to draw simple shapes on the canvas and combine them to make interesting graphics like fractals. In this lab you will continue with graphics, exploring how arbitrary images can be encoded as two-dimensional lists of pixels (where each pixel is itself a list) and manipulated using iteration.

When you are done you should be able to do the following:

  1. Understand how a two-dimensional list of pixels maps to the image on the screen.
  2. Create a list representing a simple image.
  3. Be fluent with code handling two-dimensional lists.
  4. Perform simple geometric transformations on an image representation, like flipping horizontally or vertically.
  5. Perform simple color transformations on an image representation.

Activities

  1. Create a lab12 directory. Download the files image.py and nautical.ppm to this directory and cd to this directory. Run python3 and display the nautical flag alphabet on a Canvas by evaluating the following Python expressions:
    from image import *
    b = read_ppm("nautical.ppm")
    start_image(250)
    draw_image(b, 0, 0)
    
            

  2. The nautical alphabet image is 200 by 136 pixels. It is encoded as a list of lists. The main list contains 200 rows. Each row is a list containing 136 pixels. Each pixel is a list of form [r,g,b], where r, g, and b are red, blue, and green intensity values from 0 to 255. Try the following expressions:
    len(b)
    len(b[0])
    b[0][0]
    b[100][100]
    
    Knowing that an image is a list of rows, write a function flip_image(image) that flips image upside-down and plots the result. (Hint: If L is a list, L.reverse() will reverse the elements of the list.) Try flipping the nautical alphabet image upside-down.

  3. Write a function c_flag to create a little 5-by-6 pixel image encoding the flag for the letter C in the nautical flag alphabet. Refer to the RGB Color Table to get the codes for red and navy blue. Since this image is very small, you will want to magnify it for display purposes by writing draw_image(c_flag(), 0, 0, 20).

  4. Write a function swap_bg(image) that swaps the blue and green values of every pixel in the image, and plots the result. Test it on the nautical flag alphabet image. What happens if you call swap_bg repeatedly on the same image?

  5. Write a function grayscale(image) that converts an image to grayscale by setting each pixel's r, g, and b values to their average value (r+g+b) // 3, and plots the resulting grayscale image. Test your function on the nautical flag alphabet image. NOTE: Make sure you use integer division.

  6. Challenge problem: write a function transpose that flips an image along the major diagonal, so that the rows become columns and the columns become rows, and plots the result.