Spring2008.CSCI180Homework3 History

Show minor edits - Show changes to markup

Added lines 96-101:

Submit the following:

  1. the Python program
  2. the original image
  3. the (result) edge-detected image
Changed line 2 from:

Due Date: Monday, Mar. 31, 2008\\

to:

Due Date: Wednesday, Apr. 2, 2008\\

Changed line 2 from:

Due Date: Friday, Mar. 28, 2008\\

to:

Due Date: Monday, Mar. 31, 2008\\

Changed lines 100-102 from:

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

This image is probably the most widely used test image for all sorts of image processing algorithms. (Source: Wikipedia)

to:

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

Changed lines 100-101 from:

http://rsb.info.nih.gov/ij/images/lena.jpg

to:

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

Changed lines 98-99 from:

Use the following image for testing:

to:

For testing, use the following image:

Changed line 102 from:

This image is probably the most widely used test image for all sorts of image processing algorithms.

to:

This image is probably the most widely used test image for all sorts of image processing algorithms. (Source: Wikipedia)

Changed lines 100-102 from:

http://rsb.info.nih.gov/ij/images/lena.jpg

to:

http://rsb.info.nih.gov/ij/images/lena.jpg

This image is probably the most widely used test image for all sorts of image processing algorithms.

Changed lines 94-100 from:

Upload your Python program to WebCT.

to:

Upload your Python program to WebCT.

Test

Use the following image for testing:

http://rsb.info.nih.gov/ij/images/lena.jpg

Changed lines 13-14 from:

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

to:

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

Changed lines 17-18 from:

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

to:

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

Changed lines 25-28 from:

Include your design (pseudocode) as comments in your program.

The following comments should appear in your program as the first lines in the file. Items in angle brackets are either to be removed or replaced with what is specified within the brackets:

to:

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

Changed line 28 from:
  1. sunset.py version 1.0 14-Mar-2008 (bzm)
to:
  1. sunset.py version 1.0 14-Mar-2008 Bill Manaris
Changed lines 39-70 from:

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

  1. create a new, empty grayscale image
  1. im = Image.new("L", size = (width, height))
  2. create a new, empty color (RGB) image

imageNew = Image.new("RGBA", size = (width, height))

  1. access individual pixels

pixelsOld = imageOld.load() pixelsNew = imageNew.load()

  1. 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) 
  1. display the resultant image

imageNew.show()

  1. im.save("test.jpg","JPEG")
to:

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
   imageNew.show()

   #im.save("test.jpg","JPEG")

main()

Added lines 76-87:

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

Changed line 30 from:
  1. PIL_demo.py version 1.0 14-Mar-2008 (Bill Manaris)
to:
  1. sunset.py version 1.0 14-Mar-2008 (bzm)
Changed lines 32-35 from:
  1. This program demonstrates how to create a color (or grayscale) image
  2. in Python (using the Python Imaging Library). It shows how to access
  3. individual pixels and store color in RGB. Finally, it shows how to
  4. display the result image.
to:
  1. This program demonstrates how to create a sunset effect by reducing the
  2. green and blue components of an image.
  3. There is also commented code that allows to open an existing image.
Added line 38:

from math import *

Changed lines 41-47 from:

width = 200 # image width height = 300 # image height

im = Image.new("RGBA", size = (width, height)) # create a blank color image

  1. write to individual pixels

pixels = im.load()

to:

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

  1. create a new, empty grayscale image
  1. im = Image.new("L", size = (width, height))
  2. create a new, empty color (RGB) image

imageNew = Image.new("RGBA", size = (width, height))

  1. access individual pixels

pixelsOld = imageOld.load() pixelsNew = imageNew.load()

  1. loop through all pixels (one column at a time, and within each column, top to bottom)
Changed lines 61-64 from:
          # store R, G, B
          pixels[x, y] = (x, y, x+y)

im.show()

to:
          # 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) 
  1. display the resultant image

imageNew.show()

  1. im.save("test.jpg","JPEG")
Changed line 81 from:

Upload your Python program to WebCT.

to:

Upload your Python program to WebCT.

Added lines 1-61:

Assigned Date: Friday, Mar. 21, 2008
Due Date: Friday, Mar. 28, 2008
Due Time: noon

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

Purpose

This assignment focuses on image processing using Python.

Assignment

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

Design

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

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

Documentation

All identifiers should be meaningful.

Include your design (pseudocode) as comments in your program.

The following comments should appear in your program as the first lines in the file. Items in angle brackets are either to be removed or replaced with what is specified within the brackets:

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

  1. PIL_demo.py version 1.0 14-Mar-2008 (Bill Manaris)
  2. This program demonstrates how to create a color (or grayscale) image
  3. in Python (using the Python Imaging Library). It shows how to access
  4. individual pixels and store color in RGB. Finally, it shows how to
  5. display the result image.

import Image

width = 200 # image width height = 300 # image height

im = Image.new("RGBA", size = (width, height)) # create a blank color image

  1. write to individual pixels

pixels = im.load() for x in range(width):

     for y in range(height):
          # store R, G, B
          pixels[x, y] = (x, y, x+y)

im.show() (:sourcend:)

Grading

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

Submissions

Upload your Python program to WebCT.