CS 4 Computer Science Bootcamp

CS 4 Computer Science Bootcamp
Winter 2018

Lab 03

In this lab, you will experiment with several image and pixel functions, such as making an image grayscale and halving or doubling an image.

You will be using the Python module cImage.py, which we studied in class. "Right-Click and Save" this module to a folder on your desktop. Also, I wrote several functions for you to use, which are all found in a single program file test.py. Save this file into the same folder.

I also have three images for you to experiment:

Save these images into the same folder as well. You may also experiment with your own images, however they need to be GIF files.

The module cImage.py comes (built-in) with the following image and pixel functions:

  • im1 = FileImage("leo1.gif"): This function creates an abstract link to the image file "leo1.gif" and represents it in the program as "im1".
  • im2 = EmptyImage(n,m): This function creates an empty image data structure, which is of size nxm.
  • n = im1.getWidth(): This function returns the width (the number of columns) of the image "im1".
  • m = im1.getHeight(): This function returns the height (the number of rows) of the image "im1".
  • mywin = ImageWin("Test",n,m): This function creates a window of size nxm to draw images. The title of the window is "Test".
  • im1.draw(mywin): This function draws the image "im1" inside the window "mywin" starting from the left-top corner. If the image is smaller than nxm, the rest of the window will be empty. If the image is larger, it will be clipped.
  • im1.save("new.gif"): This function saves the image "im1" into the file "new.gif".

  • p = im1.getPixel(i,j): This function receives the pixel in the (i,j) location of the image "im1" and places it in the pixel variable "p".
  • im2.setPixel(i,j,p): This function places the pixel p in the (i,j) location of the image "im2".
  • r = p.getRed(): This function returns the red intensity of the pixel "p".
  • g = p.getGreen(): This function returns the green intensity of the pixel "p".
  • b = p.getBlue(): This function returns the blue intensity of the pixel "p".

Lab Instructions

The first 3 lines of the test program "test.py" contains statements to import the "cImage.py" functions to your development platform in order to use them.
import os
os.chdir("/Users/koc/Desktop/abc")
from cImage import *
Change the file path from above to the one you will be using.

The test program "test.py" has several functions in it. You will be experimenting with them in order to learn how they work. After applying these functions, draw or save the produced images and check to see if they accomplish what they are intended to do.

  • im2 = MakeGrayImage(im1): receives the image im1 with an arbitrary size of nxm, and returns im2 which is the grayscaled image of im1.
  • im2 = MakeNegativeImage(im1): receives the image im with an arbitrary size of nxm, and returns im2 which is the negative image of im1.
  • im2 = MakeHalfImage(im1): receives the image im1 with an arbitrary size of nxm, returns im2 which has the size of (n/2)x(m/2).
  • im2 = MakeDoubleImage(im1): receives the image im1 with an arbitrary size of nxm, returns im2 which has the size of (2n)x(2m).

This lab does not require a report. A demo to the TA is sufficient.