MCS-177 Project 7: Image Processing


Start: Tuesday 4/15; Due: Monday 4/28, by the beginning of class

Overview

In this project, you will learn how a computer can modify images. You will be programming transformations similar to those built into programs like Photoshop.

You will work on this project individually.

Specific tasks

  1. Download an image that you would like to work with. If the image is not in the GIF format, you can convert the image to the GIF format using Preview. Double-click on the image you downloaded to open it in Preview. Go to File -> Export. Hold down the "option" key and click on the format. Choose "GIF" from the list and click on "Save".

    If the image is big, try to reduce the size before saving. You can go to Tools -> Adjust Size to change the size of the image

  2. Do Exercise 6.17 (on page 204), which calls for writing a pixel function for sepia toning. Name the procedure sepiaTone. Regarding the hint in that exercise, first think about how you would convert the RGB values to integers. Then think about how you would make sure each value is no larger than 255, using the min function.

  3. When you take pictures, you sometimes find that the pictures are too bright or too dark to see anything in them. Gamma correction allows you to compress or expand the range of luminance in images. It changes the luminance following the power-law expression:

    However, pixels in our images are in RGB, not in YUV. The following formulas will help you convert between RGB and YUV values.

    1. Convert the RGB values to YUV.
    2. Y = 0.00117 * R + 0.00230 * G + 0.000447 * B
      U = -0.000577 * R - 0.00113 * G + 0.00171 * B
      V = 0.00241 * R - 0.00202 * G - 0.00039 * B

    3. Adjust Y using the gamma formula discussed above.

      Ynew = (Yold)gamma

    4. Convert YUV back to RGB.
    5. R = 255 * (Y + 1.13983 * V)
      G = 255 * (Y - 0.39465 * U - 0.58060 * V)
      B = 255 * (Y + 2.03211 * U)

    Modify the RGB values if necessary to make them no smaller than 0 and no larger than 255.

  4. Write the contract and implementation for a procedure transformPixels that takes an image and a procedure that transforms a pixel and returns a new image with all the original pixels transformed by the given procedure.
  5. Write the contract and implementation for a procedure rotateImage90 that takes an image and returns an image of the original image rotated 90 degrees clockwise.

  6. Write the contract and implementation for a procedure drawImages that takes a title and a list of images and draws the images on a new canvas. The images will be drawn onto the canvas side-by-side horizontally with 1-pixel-wide gaps. Make sure the canvas is just wide and tall enough (not unnecessarily big) and the title is shown correctly. Also, make sure the window closes on a mouse click.

    Example:

    Assume darkKnight refers to the following image object:

    >>> darkKnight = FileImage("dark.knight.gif")
    >>> drawImages("Dark Knight", [transformPixels(darkKnight, gammaHalf), transformPixels(rotateImage90(darkKnight), gammaTwo)])

    should show the following window.

  7. Assume princessBride refers to the following image object:

    >>> princessBride = FileImage("princess.bride.gif")
    >>> drawImages("Princess Bride", [princessBride, rotateImage90(rotateImage90(princessBride))])

    should show the following window.

    The words "princess bride" look the same in the two images because this is an ambigram for the cover of the 20th anniversary edition of Princess Bride.

  8. Write a Python statement (like the one in the example above) to show the original image and at least two manipuations of the image in the same window using the procedure drawImages.

Submitting your work

You will be submitting your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, you will need to submit the following file:

Grading

You will earn one point for each of the following accomplishments:

  1. You submitted GIF images you used
  2. You wrote a correct contract and docstring for the procedure sepiaTone.
  3. Your sepiaTone correctly applies sepia tone.
  4. Your sepiaTone uses min function.
  5. You wrote a correct contract and docstring for the procedure gammaTwo.
  6. Your gammaHalf correctly applies the gamma correction with 2
  7. You wrote a correct contract and docstring for the procedure gammaHalf.
  8. Your gammaHalf correctly applies the gamma correction with 0.5
  9. Your gammaHalf and gammaTwo do not repeat codes.
  10. You wrote a correct contract and docstring for the procedure transformPixels.
  11. Your transformPixels applies the transform procedure to all the pixels in the given image.
  12. You wrote a correct contract and docstring for the procedure rotateImage90.
  13. Your rotateImage90 rotates the image 90 degrees clockwise correctly.
  14. You wrote a correct contract and docstring for the procedure drawImages.
  15. Your drawImages places the title for the image window.
  16. Your drawImages calculates the correct width and height for the image window.
  17. Your drawImages places images at the right positions with gaps inbetween them.
  18. Your drawImages can place an arbitrary number of images.
  19. You use descriptive names for parameters and variables used in your procedures.
  20. Your procedures are not unnecessarily complex.