CS 100 - Computer Literacy, Spring 2005

Lab 7 Solution

Horizontal collage with three copies

Let's look at solution in simplistic form. The original image is made up of pixels arranged in a table as shown below (the column and row location for each pixel is also shown). The new image is a collage in which three copies of the original image are placed side by side horizontally. The picture will look like following (notice the colors and how they are copied in the new image, we will discuss this in detail later):


(1,1) (2,1) (3,1) (4,1)
(1,2) (2,2) (3,2) (4,2)
(1,3) (2,3) (3,3) (4,3)
Original Image


(1,1) (2,1) (3,1) (4,1) (5,1) (6,1) (7,1) (8,1) (9,1) (10,1) (11,1) (12,1)
(1,2) (2,2) (3,2) (4,2) (5,2) (6,2) (7,2) (8,2) (9,2) (10,2) (11,2) (12,2)
(1,3) (2,3) (3,3) (4,3) (5,3) (6,3) (7,3) (8,3) (9,3) (10,3) (11,3) (12,3)
New Image

Here are some important points:

Let's try to figure out how the colors are copied from original image to new one:

Pixel in original image Corresponding pixels in new image
(2,1)
(2,1) (6,1) (10,1)
(1,2)
(1,2) (5,2) (9,2)
(4,2)
(4,2) (8,2) (12,2)
(3,3)
(3,3) (7,3) (11,3)
So for each original pixel (x,y), in new image the row number is same as the original one and column numbers are x, x+origWidth and x+origWidth+origWidth.
(x,y)
(x,y) (x + origWidth,y) (x + origWidth + origWidth,y)

So we can modify the original program given as part of Lab 7 as follows (notice the parts striked out, these are changed in new function):

def duplicateHorz():
def triplicateHorz():
  
  #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
  newWidth = origWidth * 3
  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 three 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 copy to c
      left_pixel = getPixel(new_pic, x, y)
      setColor(left_pixel, c)
      pixel1 = getPixel(new_pic, x, y)
      setColor(pixel1, c)

      # set the color to the pixel in the middle copy to c
      pixel2 = getPixel(new_pic, x+origWidth, y)
      setColor(pixel2, c)

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

  #Now display the new image
  show(new_pic)

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

Program for horizontal collage (with three copies)

Vertical Collage with two copies

Let's look at problem in the same way as before:

(1,1) (2,1) (3,1) (4,1)
(1,2) (2,2) (3,2) (4,2)
(1,3) (2,3) (3,3) (4,3)
Original Image
(1,1) (2,1) (3,1) (4,1)
(1,2) (2,2) (3,2) (4,2)
(1,3) (2,3) (3,3) (4,3)
(1,4) (2,4) (3,4) (4,4)
(1,5) (2,5) (3,5) (4,5)
(1,6) (2,6) (3,6) (4,6)
New Image

Here are some important points:

Let's try to figure out how the colors are copied from original image to new one:

Pixel in original image Corresponding pixels in new image
(2,1)
(2,1) (2,4)
(1,2)
(1,2) (1,5)
(4,2)
(4,2) (4,5)
(3,3)
(3,3) (3,6)
So for each original pixel (x,y), in new image the column number is same as the original one and row numbers are y, y+origHeight.
(x,y)
(x,y) (x, y + origHeight)

So we can modify the original JES function as follows:

def duplicateHorz():
def duplicateVertical():
  
  #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
  newWidth = origWidth
  newHeight = origHeight
  newHeight = origHeight * 2

  #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 top copy to c
      left_pixel = getPixel(new_pic, x, y)
      setColor(left_pixel, c)
      top_pixel = getPixel(new_pic, x, y)
      setColor(top_pixel, c)

      # set the color to the pixel in the bottom copy to c
      right_pixel = getPixel(new_pic, x+origWidth, y)
      setColor(right_pixel, c)
      bottom_pixel = getPixel(new_pic, x, y+origHeight)
      setColor(bottom_pixel, c)

  #Now display the new image
  show(new_pic)

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

Program for vertical collage (with two copies)