CS2 Computational Thinking for Scientists - Lab 03

CS2 Computational Thinking for Scientists - Lab 03

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

You will be using the Python module cImage.py, which we have been studying 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 on Thursday, January 22

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
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).

Lab on Thursday, January 29

The first part of today's lab involves understanding the GIF standard and parsing (understanding the bytes) of different GIF images. We will be experimenting using the Python module cImage.py, as before.
  • Consider the small image small.gif. Read the image file hex data using your hex editor, and discover its properties. Outline (draw) the image in your report, and write the data for each image component (GIF Signature, Version Number, Size, Packed Byte (Flags: Color Table Flag, Color Table Size), Color Table and its Color Values, Graphics Control Extension, Image Description, LZW Code Size and the Number of Bytes, LZW Encoded Image File, End of Image Marker, and End of File Marker)
  • Create another similar small image (of size 8x8 or 10x10 or 16x16) with few (2, 3, 5, or 8 colors) using Python. I have an example code make.py which you can edit as you wish. You may also create the image using some other image editor (such as Paint) or an online tool. You may pick particular colors using RGB Chart. After creating and saving the image, examine the GIF file using the hex editor and write in your report the data for each image component, similar to the previous section.
The second part of today's lab involves understaning image watermarking.
  • Consider the image leo1.gif (size 500x500). Watermark this image using the image kate.gif (size 500x500) using the example code water.py. Watermark leo1.gif using kate.gif with bit sizes 1 to 7, and for each case extract the watermarking image from the watermarked image.
  • Repeat this experiment by watermarking leo1.gif with the UCSB logo ucsb.gif (size 500x500) and the Sun image sun500.gif (size 500x500)
  • In your report, describe the watermarking experiments and explain the role of the bit size in each of the images: original, watermarking, watermarked, and extracted.

The lab report is due 5pm, Friday Jan 30.