CS 100 - Computer Literacy, Spring 2005

Lab 5

This lab assignment will have you to crop an image and then upload an image to lab 5 web page on the CS 100 Swiki.

Cropping an image

Cropping of an image is to take a certain part of image and discard the rest of the image. It is one of the essential functions in image processing/manipulation. But before we get into details of cropping an image, it is necessary to learn some basics about how the image is stored.

There are various formats to store the image (JPEG, GIF, PNG etc). JES has its own format. In JES, each image is composed of several "picture elements", or "pixels" for short. In an image, pixels are arranged in a table as below:

 pixel   pixel   pixel 
 pixel   pixel   pixel 
 pixel   pixel   pixel 
 pixel   pixel   pixel 

The height of the image is defined as how many rows of pixels there are in image. The width of an image is the number of columns of pixels in the image. For the image representation shown above, the height is 4 and width is 3. Every pixel has a color and all pixels put together defines the way image looks. Now let's have a look at the program that allows user to pick up and image and display its width and height. Finally, it will display the image:

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

  #print the file name user chose
  print file

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

  #display height and width of the image
  print "height of image ", getHeight(pic)
  print "width of image ", getWidth(pic)

  #Now display the image
  show(pic)

Program to pick up and display an image

Now let's learn something about cropping an image. Here is the method that we shall use for cropping an image. We shall allow the user to pick up and image. We shall create an empty image which will have the cropped image. Then we shall copy the pixels from the part of the original to the new image. Here what we mean by copying the pixels is nothing but copying the colors of pixels from original image to the new image. Here is a program to do this (this was discussed in the class):

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

  #print the file name user chose
  print file

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

  # decide the size of new image
  newWidth = 100
  newHeight = 150

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

  # now decide starting with which part of image
  # we should crop the image
  start_x = 50
  start_y = 100

  # so we will crop the image starting from pixel
  # at row 100 column 50 and size of the image would be
  # 100 columns and 150 rows.

  for x in range(1, newWidth+1):
    for y in range(1, newHeight+1):

      # get pixel from original image
      orig_pixel = getPixel(pic, x+start_x, y+start_y);

      # get pixel from new image
      new_pixel = getPixel(new_pic, x, y);

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

      # set the color of new pixel to c
      setColor(new_pixel, c)

  #Now display the new image
  show(new_pic)

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

Program to crop an image

Lab Assignment 5

Due: Monday 2/21/2005 by 12:00 noon

For this assignment, you take any arbitrary image from the webpages given below. And you have to produce two cropped images by modifying the program shown above. The cropped images should be having different sizes and should be from different part of the same original image. So for this you will need to modify the program twice, first to get one cropped image and second to get another cropped image which is from different part of the original image and also of different size. You are to post these images on lab 5 page. You should put your name before you post the image. If you don't know how to post an image on Swiki page, please refer to Lab 4.

You are also to submit the code that you used to crop your image by emailing it to the CS 100 course account at i100@cs.uic.edu. You are to explain in the email what sort of modifications to the program were done to get the results posted on the lab 5 page on CS 100 Swiki.

You may choose any image to crop except for the UIC Banner image (since this was done in class). There are two collections of images that you can use for this assignment: