CSCI 150: Prelab 4 Answer Key

Images and Functions

In this optional prelab (you do not have to turn this in, it is just an additional resource), we will formulate some of the ideas necessary to complete Lab 4.

Function Practice

Q1. What is the output of the following program?

    def main() : 
      blarg(5)
      blorf(3)
    
    def blarg(x) :
      print(x*x)
      print(x)
    
    def blorf(y) :
      blarg(y+1)
      print(y)
      blarg(y-1)

    main()
								  

Answer:

    25
    5
    16
    4
    3
    4
    2
Q2. What is the output of the following program?

    def main() :
      x = 2, y = 7
      print(x, y, end="")
      x = blunk(x, y)
      print(x, y, end="")
    
    def blunk(y, x) :
      print(x, y, end="")
      y = y * y
      x = x//2
      print(x, y, end="")
      return x*y

    main()
								  

Answer:

    2 77 23 412 7

Image Manipulation

In this lab, you will create a nifty program that reads in an image and does a sequence of modifications to that image, as specified by the user. Things like negating the image, blurring it, changing the contrast, etc.

Describe the Problem:

Your program should prompt the user to enter the filename of an image to edit. It should then repeatedly prompt them for filters to apply, and show the cumulative effect of these filters on the specified image.

Understand the Problem:

original

Consider the image of crayons above. There are numerous transformations we might apply to this image. For the lab, you'll be creating a program capable of the following operations:
  1. Make Negative
  2. Make Grayscale
  3. Flip Horizontally
  4. Scroll Horizontally
  5. Zoom
  6. Your Choice: (Pick one -- Blur, Posterize, Increase Contrast, etc.)
Examples of these operations are given below.
Original Image:
original
Negative: Greyscale:
flip mirror
Flip: Scroll:
scroll negative
Zoom:
zoom
Other Ideas
Blur: Posterize:
poster scroll
Find Edges: Tiled:
find edges tiling
Shear: Bizarre:
shear bizarre

Design an Algorithm:

You will want to write pseudocode before you start writing functions. Details for some of these operations are given below.

  • Negative: The negative of an image is creating by inverting each color channel. So if the red value of a pixel were 255, it should become 0. If it were 254, it should become 1, and so on, down to 0, which should become 255. Similarly for green and blue.

  • Grayscale: Shades of gray have the same red, green and blue value. To convert an image to grayscale, you want to set the red, green and blue values all to the average value of the three channels of the original pixel.

  • Scrolling: Scrolling should ask the user to specify some number of pixels, and should then shift the image that many to the right. Pixels that would fall off the edge of the image should wrap around to the other size. Modular arithmetic may come in handy here.

  • Zoom: This method result in an image of the same size as the original, but consist of the center of the image blown up by a factor of 2. So if the image has width w and height h, zooming should expand the middle w/2 by h/2 region to fill the whole picture.

  • Blur: When you blur an image, you set the color of each pixel to be the average of the 9 pixels in the 3 x 3 square centered at that pixel (i.e. the average of the original pixel and its original 8 neighbors). You'll probably want to create a new Picture object; otherwise, you'll be adjusting pixel values that you'll need for subsequent calculations. Be careful at the borders, not all pixels have 8 neighbors!

  • Posterize: A typical pixel can have one of 256 value for each color channel. In a posterized image, this number is drastically decreased. Each color channel value should be rounded to the nearest multiple of 32.
In the lab, you'll be writing a program that performs many of the above transformations on images. In preparation, you'll be generating pseudocode to perform some of these operations. As an example, we consider the task of removing all red from an image, and give a pseudocode solution is given below. Give your own pseudocode solutions to the filters listed in Q3, Q4, and Q5 below.

Hints:
  • For some of these operations, you may want to create a new image rather than by simply modifying the existing image. You may assume you can generate a copy of an image.

  • You can not change the position of a pixel. You can, however, read the r, g and b values for any pixel, and you can set the r, g and b values of any pixel.
Ex. Remove all red from an image.
  Sample answer:

  loop over x values from 0 to W - 1 inclusive (You can assume W is the width)
      loop over y values from 0 to H - 1 inclusive (You can assume H is the height)
	  change the red value of the pixel at (x,y) to 0
								    
Q3. Negate the image.

Answer:

  loop over x values from 0 to W - 1 inclusive (You can assume W is the width)
      loop over y values from 0 to H - 1 inclusive (You can assume H is the height)
          get the red, green, and blue values of the pixel at (x, y) as r, g, b
	  change the red, green, and blue values of the pixel at (x, y) to 255 - r, 255 - g, and 255 - b
								    
Q4. Convert the image to grayscale.

Answer:

  loop over x values from 0 to W - 1 inclusive (You can assume W is the width)
      loop over y values from 0 to H - 1 inclusive (You can assume H is the height)
          get the red, green, and blue values of the pixel at (x, y) as r, g, b
	  change the red, green, and blue values of the pixel at (x, y) to the average of r, g, and b 
								    
Q5. Scroll the image to the right by d pixels.

Answer:

  make a copy of the picture so we do not lose any information when we copy pixels to a new location

  loop over x values from 0 to W - 1 inclusive (You can assume W is the width)
      loop over y values from 0 to H - 1 inclusive (You can assume H is the height)
          find the new x value where you want to copy the pixel at (x, y) to: new_x = (x + d) % W
          get the red, green, and blue values of the pixel at (x, y) in the copy of the picture as r, g, b
	  change the red, green, and blue values of the pixel at (new_x, y) in the original picture to r, g, b
								    

Honor Code

If you followed the Honor Code in this assignment, write the following sentence attesting to the fact at the top of your homework.

I affirm that I have adhered to the Honor Code in this assignment.