CS 100 - Computer Literacy, Spring 2005

Lab 7

This assignment will have you take an image and create a new image that will display the original picture multiple times. For example, take the following image of George Washington.

This image shown in twice side-by-side/horizontally would be:

Duplicating an Image Side-by-Side

In order to create an image that will display an image twice side-by-side, we first need to create a blank canvas using makeEmptyPicture() that will be be twice as wide as the original image but have the same height. So we will need to get the width and height from the original image so we can determine the size needed for the duplicated image.

Next we need a loop to access each pixel from the original image. Each pixel must be put into the duplicated image twice, once on the left side and once on the right side. We can use getColor() to get the pixel's color information from the original image and use setColor() twice to put that color information into the duplicated image. The only remaining question is:

"Where does the color information get put in the duplicated image?"

To answer this question let us assume we are dealing with an original image of 100x150 pixels. Hopefully by understanding what is done with this example, we can determine how to deal with this for any image.

The following JES code shows how this is done:
def duplicateHorz():
  
  #Allow the user to pick up an image file
  file = pickAFile()

  #now convert the image in JES format
  pic = makePicture(file)

  # get the size of the originalimage
  origWidth = getWidth(pic)
  origHeight = getHeight(pic)

  # determine the size of new image
  newWidth = origWidth * 2
  newHeight = origHeight

  #now creat a new image in JES format
  new_pic = makeEmptyPicture(newWidth, newHeight)

  # Loop for each pixel in the original image.
  # Put that pixels color information in two positions
  # in the new duplicated image.

  for orig_pixel in getPixels(pic):

      # get X and Y positions of pixel from new image
      x = getX(orig_pixel)
      y = getY(orig_pixel)

      # get the color from original pixel
      c = getColor(orig_pixel)

      # set the color to the pixel in the left side to c
      left_pixel = getPixel(new_pic, x, y)
      setColor(left_pixel, c)

      # set the color to the pixel in the right side to c
      right_pixel = getPixel(new_pic, x+origWidth, y)
      setColor(right_pixel, c)

  #Now display the new image
  show(new_pic)

  # save the new image
  writePictureTo(new_pic, pickAFile())

Program to duplicate an image within another image

Lab Assignment 7

Due: Monday 3/7/2005 by 12:00 noon

Modify the above code in two different ways.

  1. Write a function triplicateHorz() that will produce three images side-by-side/horizontally. Doing this to the George Washington image we would get:
  2. Write a function duplicateVert() that will produce two images one on top of the other (i.e. vertically). Doing this to the George Washington image we would get:
Both of these functions are to be written in the same python file. You are to submit this function by emailing it to the CS 100 course account at i100@cs.uic.edu. Be sure to use a meaningful subject line on the email.

We would like to see any resulting images posted on the lab 7 page on CS 100 Swiki.

Try to use a small image when running your program. Some images from our textbook can be found at http://www.cs.uic.edu/~i100/images/other/index.html.