Spring 2008»CSCI 180 Homework 3

CSCI 180 Homework 3

Assigned Date: Friday, Mar. 21, 2008
Due Date: Wednesday, Apr. 2, 2008
Due Time: noon

Last modified on March 31, 2008, at 01:53 PM (see updates)


This assignment focuses on image processing using Python.


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


First write out your algorithm in English. Make sure it does the right thing. Then translate it into Python. Remember the aphorism:

"20 hours of coding can save you two hours of design."


All identifiers should be meaningful.

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

# sunset.py  version 1.0    14-Mar-2008      Bill Manaris
# This program demonstrates how to create a sunset effect by reducing the
# green and blue components of an image.
# There is also commented code that allows to open an existing image.

from math import *
import Image

def main():
   filename = raw_input("Enter image filename: ")
   imageOld = Image.open(filename)    # open the provided image

   imageOld.show()                    # display it

   width, height = imageOld.size      # remember its dimensions

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

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

   # access individual pixels
   pixelsOld = imageOld.load()
   pixelsNew = imageNew.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 = pixelsOld[x, y]

             # ...to create an appropriate color
             # for the corresponding pixel in the new image
             pixelsNew[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 below.


Upload your Python program to WebCT.

Submit the following:

  1. the Python program
  2. the original image
  3. the (result) edge-detected image


For testing, use the following image: