Fall2008.FYSM117Homework5 History

Show minor edits - Show changes to markup

Added lines 1-107:

Assigned Date: Tuesday, ??, 2008
Due Date: Tuesday, ??
Due Time: 9:25am

Last modified on November 19, 2008, at 04:30 PM (see updates)

This is an individual assignment. You must work on your own (as per our honor code policy).

Purpose

This assignment focuses on image processing using Python.

Assignment

Write a Python program to do simple edge detection of images. Our goal is to capture abrupt changes of luminosity between consecutive pixels.

Design

Think about the algorithm. First outline the steps to perform this task on paper (e.g., first, input the image, etc.). If you cannot write it in English, you cannot write in Python.

Documentation

All variable names should be meaningful.

Include your design as comments in your program. Use the program below as an example on style. Your program should look similar to this (updated appropriately):

(:source lang=Python tabwidth=3 -trim :)

  1. sunset.py version 1.0 14-Mar-2008 Bill Manaris
  2. TASK: Modify an image to create a sunset effect (by reducing the green and blue
  3. components of every pixel).
  4. INPUT: A JPEG image specified by the user.
  5. OUTPUT: The modified JPEG image.

from math import * import Image

def main():

   filename = raw_input("Enter image filename: ")
   originalImage = Image.open(filename)    # open the provided image

   originalImage.show()                    # display it

   width, height = originalImage.size      # remember its dimensions

   ## create a new, empty grayscale image
   #im = Image.new("L", size = (width, height))    

   # create a new, empty color (RGB) image
   modifiedImage = Image.new("RGBA", size = (width, height))  

   # access individual pixels
   originalPixels = originalImage.load()
   modifiedPixels = modifiedImage.load()

   # loop through all pixels (one column at a time, and within each column, top to bottom)
   for x in range(width):
        for y in range(height):

             # for each pixel in the old image, use its color (R, G, B)...
             red, green, blue = originalPixels[x, y]

             # ...to create an appropriate color
             # for the corresponding pixel in the new image
             modifiedPixels[x, y] = (red, green * 0.7, blue * 0.7) 

   # display the resultant image
   modifiedImage.show()

   modifiedImage.save("sunset.jpg","JPEG")

main() (:sourcend:)

To calculate the distance between two color (RGB) pixels, use the following function:

(:source lang=Python tabwidth=3 -trim :) def distance(pixel1, pixel2):

    """This function returns the color distance between two RGB pixels."""

    r1, g1, b1 = pixel1
    r2, g2, b2 = pixel2

    return sqrt((r1-r2)**2 + (g1-g2)**2 + (b1-b2)**2)

(:sourcend:)

Grading

Your grade will be based on design and style as well as correctness of result. For style, see sample code above.

Submissions

Submit the following:

  1. At the beginning of class on the due date, bring a paper printout of your code along with a printout of the original image, and a printout of the output (edge-detected) image.
  2. Email me a copy of your source code. The subject must read "FYSM: Homework 4".

Test

For testing, use the following image:

http://www.hermann-uwe.de/files/images/blue_flower.preview_0.jpg