CS 101 - Introduction to Computing, Spring 2007

Lab 6

This lab assignment will have you write at least two functions in jython that will create a new image that will show a portion from another image. This technique is called cropping.

Reminder

The program Jython Environment for Students (or JES) is the development environment that we will use for this lab.

In the ACCC Labs that have JES, it can be found by:

  1. Clicking on Start
  2. Then click on All Programs
  3. Then click on Class Applications
  4. Then click on Engineering
  5. Finally click on Jython Environment for Students

Cropping an image

Cropping of an image is to take a certain part of image and discard the rest of the image. Once the user has picked an image, we shall create an empty image which will store the resulting cropped image. Then we shall copy the pixels from the part of the original to the new image. "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:

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 create 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 6

Due: Tuesday 2/27/2007 by 11:59 pm

Create a file using JES that will:

  1. Contain a comment indicating

  2. Contain a JES function that will

  3. Contain a JES function called cropImage () that will: This function may call other functions that are built-in to JES or that you have written. Note that the code give above is close to what is needed here but it is not exact. If any of the verify checks fail, print out a good error message and return from the function.

Submittal of the Lab Assignment

Comments on the ACCC Labs

On the computers in the ACCC Labs there should an H: drive. This drive is actually a networked connection to your own file space maintained by the ACCC. No matter what machine you use or what lab you are in, the H: drive will access the same file space. This means that you can save a file on the H: drive on a computer in one lab and access that same file on a computer in another lab. This can be very helpful. It is suggested that you store your python program files on your H: drive.