CS 100 - Computer Literacy, Spring 2005

Homework 7 Solution

Here is the equivalent tabular image from lab 7 solution.


(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)
(1,2) (2,2) (3,2) (4,2) (5,2) (6,2) (7,2) (8,2)
(1,3) (2,3) (3,3) (4,3) (5,3) (6,3) (7,3) (8,3)
(1,4) (2,4) (3,4) (4,4) (5,4) (6,4) (7,4) (8,4)
(1,5) (2,5) (3,5) (4,5) (5,5) (6,5) (7,5) (8,5)
(1,6) (2,6) (3,6) (4,6) (5,6) (6,6) (7,6) (8,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) (6,1) (2,4) (6,4)
(1,2)
(1,2) (5,2) (1,5) (5,5)
(4,2)
(4,2) (8,2) (4,5) (8,5)
(3,3)
(3,3) (7,3) (3,6) (7,6)
So for each original pixel (x,y), the corresponding four pixels in the new image have following property:
(x,y)
(x,y) (x + origWidth, y) (x,y + origHeight) (x + origWidth, y + origHeight)

So we can modify the original program given as part of Lab 7 solution as follows

def makeCollage():
  
  #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 * 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 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 upper left copy to c
      pixel1 = getPixel(new_pic, x, y)
      setColor(pixel1, c)

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

      # set the color to the pixel in the lower left copy to c
      pixel3 = getPixel(new_pic, x, y+origHeight)
      setColor(pixel3, c)

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

  #Now display the new image
  show(new_pic)

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

Program for collage (2x2 table)

So this one will make four copies of the original image. But what we need is color modifications in the original image. So we will do following:

So instead of setting all the pixel colors to the same color, we will first compose these four colors and put them in four pixels. This is how modified program looks:

def makeCollage():
  
  #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 * 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 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
      c1 = getColor(orig_pixel)

      # get the color components original pixel
      red = getRed(orig_pixel)
      green = getGreen(orig_pixel)
      blue = getBlue(orig_pixel)

      # compute the average
      avg = (red + green + blue) / 3

      # make black and white color
      c2 = makeColor(avg, avg, avg)

      # make color without red component
      c3 = makeColor(0, green, blue)

      # make the negative color
      c4 = makeColor(255-red, 255-green, 255-blue)

      # set the color to the pixel in the upper left copy to c1
      pixel1 = getPixel(new_pic, x, y)
      setColor(pixel1, c1)

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

      # set the color to the pixel in the lower left copy to c3
      pixel3 = getPixel(new_pic, x, y+origHeight)
      setColor(pixel3, c3)

      # set the color to the pixel in the lower right copy to c4
      pixel2 = getPixel(new_pic, x+origWidth, y+origHeight)
      setColor(pixel2, c4)

  #Now display the new image
  show(new_pic)

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

Program for collage (2x2 table) Modified Colors

Grading Criteria