Topic: Images
Related Reading: Section 3.5 of the textbook, as well as our lecture notes on creating images in Python.
For some of the problems in this assignment, you will need to use Python. You can either use a Python run-time environment, such as available on the Mac computers in RH 225, or you can use the Python simulation environment, as desired.
In addition, you will need to download and install the XnView image viewer and converter for compressing images (and possibly for viewing images, depending on your environment -- it's not needed in the Linux lab).
For these problems, be sure that you have downloaded and installed the XnView image viewer and converter for compressing (and possibly viewing) images.
Generate an entirely black image.
Use the following Python program, black_image.py, to create a 64x64 PPM image that is entirely black. Running the Python program will generate a 2-D array of RGB color values for the image. Each row of text in the output representes one row of pixels, and each set of 3 values represents the R, G, and B values for each of the 64 column pixels in that row (i.e. there are 3 * 64 values per row).
There are two ways you can run the program. The first option is to run it in a Linux console/terminal window by typing python black_image.py > black.ppm (in the directory containing the black_image.py program). This method uses the '>' operator to automatically redirect the output to the black.ppm image (so no additional work is needed to copy the output into an image file).
As an alternative, you can copy the program into the Python simulation environment and execute the program from there. Once you have generated the output, copy the header and the 64 rows of values (i.e. copy all the output except the ------ simulation is beginning ------ and ------ simulation is complete ------ lines of the output) into a text editor and save the file as black.ppm. The resulting file should be equivalent to black.ppm.
Open the black.ppm file with XnView, and it should display a small (64x64 pixel) black image. Use the Zoom In function of XnView, and zoom in a few times to get a better look at the image.
Generate a black image with a diagonal line of purple pixels.
In the inner loop in the Python program from above, change the line in black_image.py:
print "0 0 0",to:
if (x == y): print "255 0 255", else: print "0 0 0",
The resulting program should be equivalent to purple_diagonal_image.py.
Again, there are two ways you can run the program. The first option is to run it in a Linux console/terminal window by typing python purple_diagonal_image.py > purple_diagonal.ppm (in the directory containing the purple_diagonal_image.py program). This method uses the '>' operator to automatically redirect the output to the purple_diagonal.ppm image (so no additional work is needed to copy the output into an image file).
As an alternative, you can copy the program into the Python simulation environment and execute the program from there. Once again, copy the header and the 64 rows of values (i.e. copy all the output except the ------ simulation is beginning ------ and ------ simulation is complete ------ lines of the output) into a text editor and save the file as purple_diagonal.ppm. Upon viewing the image in XnView, you should see an image that looks like:
Generate a shaded image that progresses from black in the top row to bright blue in the bottom row of pixels.
In the inner loop in the Python program from above, change the line in black_image.py:
print "0 0 0",to:
print "0 0", y * (256 / rows),
The resulting program should be equivalent to blue_shaded_image.py.
Again, there are two ways you can run the program. The first option is to run it in a Linux console/terminal window by typing python blue_shaded_image.py > blue_shaded.ppm (in the directory containing the blue_shaded_image.py program). This method uses the '>' operator to automatically redirect the output to the blue_shaded.ppm image (so no additional work is needed to copy the output into an image file).
As an alternative, you can copy the program into the Python simulation environment and execute the program from there. Once again, copy the header and the 64 rows of values into a text editor and save the file as blue_shaded.ppm. Upon viewing the image in XnView, you should see an image that looks like:
Generate the outline of a green box on a blue background, with each side 10 pixels from the edge of the image.
In the inner loop in the Python program from above, change the line in black_image.py:
print "0 0 0",to:
if (((y == 10) or (y == 53)) and ((x >= 10) and (x= 53))): print "0 255 0", elif (((x == 10) or (x == 53)) and ((y >= 10) and (y <= 53))): print "0 255 0", else: print "0 0 255",
The resulting program should be equivalent to green_box_on_blue_image.py.
Again, there are two ways you can run the program. The first option is to run it in a Linux console/terminal window by typing python green_box_on_blue_image.py > green_box_on_blue.ppm (in the directory containing the green_box_on_blue_image.py program). This method uses the '>' operator to automatically redirect the output to the green_box_on_blue.ppm image (so no additional work is needed to copy the output into an image file).
As an alternative, you can copy the program into the Python simulation environment and execute the program from there. Once again, copy the header and 64 rows of output from Python into a text editor and save the file as green_box.ppm. Upon viewing the image in XnView, you should see an image that looks like:
Some additonal Python examples:
Compress an image using JPEG compression.
Download the image, Cliff_in_Clouds.bmp.
View the image in the XnView image viewing tool. Within XnView, under the File sub-menu, select Save As... This will open the Save picture window. At the bottom of this window, for the image type in Save as type:, select JPG - JPEG / JFIF. Then, before saving the file, select the Options button in the lower left-hand corner of the window. This opens the Options window. On the right side of the Options window, there's a sliding bar to select the Quality. Slide the bar to the left, until the Quality (in the small window to the right of the sliding bar) indicates 20. Then hit OK at the bottom of the window. This will close the Options window, saving the Quality level as 20 (out of 100). Finally, select Save in the Save picture window to save the image. This will create an image, Cliff_in_Clouds.jpg.
To compute the degree of compression, divide the file size of the original uncompressed file, Cliff_in_Clouds.bmp, by the file size of the new compressed file, Cliff_in_Clouds.jpg. The original size is 2359350 bytes (about 2.25 MB) and the compressed file should be 28640 bytes (about 28.0 KB). Therefore the degree of compression can be calculated as:
degree of compression = 2359350 / 28640 = 82.4In other words, the compressed file (using JPEG compression) is 82.4 times smaller than the original uncompressed file.
Compress an image using JPEG-2000 compression.
Download the image, Cliff_in_Clouds.bmp.
View the image in the XnView image viewing tool. Within XnView, under the File sub-menu, select Save As... This will open the Save picture window. At the bottom of this window, for the image type in Save as type:, select JP2 - JPEG-2000 JP2 File Format. Then, before saving the file, select the Options button in the lower left-hand corner of the window. This opens the Options window. On the right side of the Options window, there's a sliding bar to select the Quality. First select the radio button next to Quality, to make sure compression is being based on the Quality level. Then slide the bar to the left, until the Quality (in the small window to the right of the sliding bar) indicates 20. Then hit OK at the bottom of the window. This will close the Options window, saving the Quality level as 20 (out of 100). Finally, select Save in the Save picture window to save the image. This will create an image, Cliff_in_Clouds.jp2.
To compute the degree of compression, divide the file size of the original uncompressed file, Cliff_in_Clouds.bmp, by the file size of the new compressed file, Cliff_in_Clouds.jp2. The original size is 2359350 bytes (about 2.25 MB) and the compressed file should be 26897 bytes (about 26.3 KB). Therefore the degree of compression can be calculated as:
degree of compression = 2359350 / 26897 = 87.7In other words, the compressed file (using JPEG-2000 compression) is 87.7 times smaller than the original uncompressed file.
When you turn in your assignment, you must include a signed cover sheet with your assignment (you're assignment will not be graded without a completed cover sheet).
You are allowed to submit your assignment via email, but if you choose to do so, you must bring a hardcopy of your assignment along with a completed cover sheet to the instructor at the next class. (Note: Do not email the instructor any .zip file attachments, as SLU's email may not accept these emails, i.e. the instructor may not receive your email.)
Note: The problems assigned below are all very similar to one or more examples in the Practice Problems, so looking through all the Practice Problems is highly advised.
Similar to the Practice Exercises above, write a Python program that will draw a light gray image with a blue anti-diagonal line. In other words, the background should be light gray, and there should be a blue line connecting the bottom left corner to the top right corner of the image, like:
Generate the actual image file for this image, and view it (e.g. with XnView) to verify that your program works correctly.
Email this image file to the instructor, naming the file name as email_blue_antidiag.ppm, where email is your email login name (i.e. for the email address jdoe@slu.edu, the login name is jdoe, and the resulting file name would be jdoe_blue_antidiag.ppm).
Be sure to hand in the code for your Python program with the rest of your assignment.
Similar to the Practice Exercises above, write a Python program that will draw a white filled box on an orange background. In other words, the background should be orange, and there should be a solid white box centered in the middle of the image. The size and shape of the box can be of your choosing, as long as it is at least 4 pixels on a side, and the orange background is visible on all sides (i.e. the box isn't flush with the edge of the image), and no orange is visible inside the box (the box is entirely white), like:
Generate the actual image file for this image, and view it (e.g. with XnView) to verify that your program works correctly.
Email this image file to the instructor, naming the file name as email_white_box.ppm, where email is your email login name (i.e. for the email address jdoe@slu.edu, the login name is jdoe, and the resulting file name would be jdoe_white_box.ppm).
Be sure to hand in the code for your Python program with the rest of your assignment.
Similar to the Practice Exercises above, write a Python program that will draw an image with four equal-sized quadrants of four different colors. You may use any colors you choose, as long as the four quadrants are equal-sized and are clearly different colors, like:
Generate the actual image file for this image, and view it (e.g. with XnView) to verify that your program works correctly.
Email this image file to the instructor, naming the file name as email_color_quads.ppm, where email is your email login name (i.e. for the email address jdoe@slu.edu, the login name is jdoe, and the resulting file name would be jdoe_color_quads.ppm).
Be sure to hand in the code for your Python program with the rest of your assignment.
Similar to the Practice Exercises above, write a Python program that will draw a red diagonally-shaded image, which progresses diagonally from black in the upper left corner to bright red in the lower right corner of the image, like:
Generate the actual image file for this image, and view it (e.g. with XnView) to verify that your program works correctly.
Email this image file to the instructor, naming the file name as email_red_diag_shaded.ppm, where email is your email login name (i.e. for the email address jdoe@slu.edu, the login name is jdoe, and the resulting file name would be jdoe_red_diag_shaded.ppm).
Be sure to hand in the code for your Python program with the rest of your assignment.
Compare the quality of JPEG and JPEG-2000 compression at different degrees of compression.
Download the image, Shed_in_Field.bmp.
Similar to the Practice Exercises, generate compressed JPEG images. Generate five different JPEG-compressed images for Quality levels 40, 20, 10, 5, 2. Include the Quality level value in the name of each file for reference (i.e. name JPEG-compressed image at Quality level 10 as Shed_in_Field_q10.jpg).
Compute the degree of compression for each of these five files. Indicate these degrees of compression, and discuss the trend with respect to the degree of compression as Quality decreases.
Also, view the five compressed images. On a scale of 1-10 (1 is worst, 10 is best) indicate how well each of the compressed images compares with the uncompressed image. Indicate your scores for each, and discuss the trend with respect to the visual quality as Quality decreases.
Download the image, Shed_in_Field.bmp.
Similar to the Practice Exercises, generate compressed JPEG-2000 images. Generate five different JPEG-2000-compressed images for Quality levels 40, 20, 10, 5, 2. Include the Quality level value in the name of each file for reference (i.e. name JPEG-2000-compressed image at Quality level 10 as Shed_in_Field_q10.jp2).
Compute the degree of compression for each of these five files. Indicate these degrees of compression, and discuss the trend with respect to the degree of compression as Quality decreases.
Also, view the five compressed images. On a scale of 1-10 (1 is worst, 10 is best) indicate how well each of the compressed images compares with the uncompressed image. Indicate your scores for each, and discuss the trend with respect to the visual quality as Quality decreases.
Overall, which compression method would you say is better? Why?
Similar to the Practice Exercises above, write a Python program that will draw the outline of a magenta triangle on a black background. The three corners of the triangle should be positioned at (row,column) positions: (10,20), (50,20), (10,50).
Email this image file to the instructor, naming the file name as email_ctr_triangle.ppm, where email is your email login name (i.e. for the email address jdoe@slu.edu, the login name is jdoe, and the resulting file name would be jdoe_ctr_triangle.ppm).
Be sure to hand in the code for your Python program with the rest of your assignment.