CS 100 - Computer Literacy, Spring 2005

Lab 6 Solution

Black and white image

As we know, images are made up of pixels. The pixels are arranged in a tabular way in an image. Each pixel in the image has a color and combination of all such pixels displays image. Moreover, the color of each pixel has three components: read, green and blue.

For a color image, if we take any pixel in the image, these three components have any arbitrary value between 0 and 255. But for a black and white image, if we pick up any pixel, all three components must have same value. Remember, this does not say that all the pixels have same red, same green and same blue color component, but given a pixel all three have same value.

Now in order to make an image black and white, we need to change the color components of each pixel such that all three components have same value for any given pixel. So here is a simple method to do it:

  For given image, go through all pixels.
  for each pixel in image:
    take red component of color of the pixel (let's call it r)
    take green component of color of the pixel (let's call it g)
    take blue component of color of the pixel (let's call it b)

    Now find average or r, g, b (let's call it avg)

    change red component of the color of the pixel to avg
    change green component of the color of the pixel to avg
    change blue component of the color of the pixel to avg
Method to convert image to gray scale

Let's convert the method in JES code:

def makeBW():
  
  #Allow the user to pick up an image file
  file = pickAFile()

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

  # Loop for each pixel in the original image.
  # And change the color components of pixel

  for pixel in getPixels(pic):

      # get the color components from original pixel
      r = getRed(pixel)
      g = getGreen(pixel)
      b = getBlue(pixel)

      # compute the average of three components
      avg = (r + g + b) / 3

      # change the color components of the pixel to avg
      setRed(pixel, avg)
      setGreen(pixel, avg)
      setBlue(pixel, avg)

  #Now display the new image
  show(pic)

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

Program to make an image gray scale

In this case, we take simple or uniformly weighted average of the three components. In other words, while computing average (avg), the weight of the red, green and blue component are the same (1/3 in this case). The assignment asks us to find a weighted average of the three components with read component having weight of 0.333, green component having weight of 0.433 and blue component having weight of 0.234 (note that the summation of the three weights is 1.0). So we can modify the program as follows (the parts striken out are to be removed from program shown above):

def makeBW():
  
  #Allow the user to pick up an image file
  file = pickAFile()

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

  # Loop for each pixel in the original image.
  # And change the color components of pixel

  for pixel in getPixels(pic):

      # get the color components from original pixel
      r = getRed(pixel)
      g = getGreen(pixel)
      b = getBlue(pixel)

      # compute the average of three components
      avg = (r + g + b) / 3
      avg = r * 0.333 + b * 0.433 + g * 0.234

      # change the color components of the pixel to avg
      setRed(pixel, avg)
      setGreen(pixel, avg)
      setBlue(pixel, avg)

  #Now display the new image
  show(pic)

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

Program to make an image gray scale