Fall 2008»FYSM 117 Homework 5

FYSM 117 Homework 5

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).


This assignment focuses on image processing using Python.


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


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.


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):

# sunset.py  version 1.0    14-Mar-2008      Bill Manaris
#   TASK: Modify an image to create a sunset effect (by reducing the green and blue
#         components of every pixel).
#  INPUT: A JPEG image specified by the user.
# 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



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

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)


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


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".


For testing, use the following image: