Fall 2008»FYSM 117 Homework 4

FYSM 117 Homework 4

Assigned Date: Tuesday, Nov. 18, 2008
Due Date: Thursday, Dec. 4
Due Time: 9:25am

Last modified on November 25, 2008, at 11:20 AM (see updates)

This is a pair-programming assignment (only two people per team, please).

Purpose

This assignment focuses on creating stereoscopic 3D images using Python, and on combining different technologies to create an interesting artifact (e.g., digital cameras).

Assignment

Write a Python program to generate anaglyph 3D images.

Anaglyph images provide a stereoscopic 3D effect, when viewed with 2-color (e.g., red-cyan) glasses.

To create an anaglyph image, you start with two images of the same subject taken from two slightly different angles.

Then you combine the two images into one. The left image RGB color is averaged and placed in the red channel of the result image. The right image RGB color is averaged and placed in both the green and blue channels of the result image (green + blue = cyan).

An alternate way that preserves some color information (but not all) is use the red channel of the left image for the red channel of the result image, and the green and blue channels of the right image for the green and blue channels, respectively, of the result image.

The stereoscopic effect is generated by the visual cortex of the brain, since each eye is getting information from the corresponding (left or right) image. This is the case in both the grayscale and color versions above.

Implementation

Step 1

First write out your Python program to combine two images into one, using one of the two algorithms above. Test it to make sure it works.

Step 2

  1. Find five artifacts that would look interesting in 3D.
  2. You need a really steady hand, or a tripod.
  3. Use a digital camera to take pictures. Take two images per artifact.
  4. Very important: The two images should be at the same horizontal level.
  5. Very important: The two images should be separated from one another by approximately the distance between your eyes.
  6. Use a program like IrfanView to save the images as JPEG and resize them to a small enough size (around 100KB each).
  7. Note: After you write the program, you should take a few more pictures and experiment with different artifacts and distances. The program is just a tool. The art is in the pictures you take, and how they look in 3D. Experiment!

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

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

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

main()
 

Submissions

Submit the following:

  1. Email me your images as attachments. Their filenames should be descriptive, e.g., guitarLeft.jpg, guitarRight.jpg. guitarAnaglyph.jpg.
    • The subject must read "FYSM 117: Homework 4".
    • The body of the message should include the full names of people on your team.
    • Also, attach your Python program.
  2. At the beginning of class on the due date, bring a paper printout of your Python program. Also, bring a printout of the original images, and a printout of the output (anaglyph) image, for each of the five artifacts you found (one page per artifact).

Grading

Your grade will be based on correctness (e.g., good 3D images), aesthetic effect (e.g., interesting images), and on design/style of your code. For style, see sample code above.

Your grade will also be based on how well you followed the above instructions.

Test

For testing, here are some more left and right images (all shot with an iPhone camera):

Credits

The idea for this assignment comes from J. Ben Schafer and Patrick A. Troy through the Mediacomp-teach mailing list.