CSE 1710.03A Programming for Digital Media
Lab 06
Due date: Oct 26, 2008 at 20:00
Making an image smaller
In the book, there is an example of a function that scales an image down,
i.e. makes it smaller. A simplified version of this code is reproduced here.
def scaledownby2(filename):
barb = makePicture(filename)
canvas = makePicture("640x480.jpg")
for targetX in range(0,222/2):
for targetY in range(0,294/2):
color = getColor(getPixel(barb,targetX * 2,targetY * 2))
setColor(getPixel(canvas,targetX,targetY),color)
return canvas
You have to modify this as follows. Note that the first four steps
are fairly simple, whereas the last step requires more work.
-
Instead of using makePicture to open an existing empty picture as canvas,
use makeEmptyPicture(), a pre-defined function that takes the width
and height of the picture to be created as arguments.
-
Moreover, modify the code so that it doesn't use magic constants
(such as 222 and 294). Instead use the size of the original picture instead.
-
If you execute show(scaledownby2("barbara.jpg")),
it should result in this picture at this point.
-
Now modify the function so that it can scale down by arbitrary integer
factors, not just by two. Rename the function to "scaledown" and add
an additional argument for the scale factor.
In other words, you should be able to execute
show(scaledown("barbara.jpg",2)).
If the scale factor passed as argument is either zero, or negative
you should print an error message stating that
"Cannot scale by this factor". Use the print command for that purpose
and don't forget to do a return immediately afterwards
(e.g. to prevent a division by zero error).
If the scale factor is too large, i.e. the result gets
smaller than a single pixel, print an error message
"Result too small" and return, similar to the previous diagnostic message.
-
Finally, note that the scaled down version has some pixelation
artefacts, due to the fact that lot's of the pixels are "thrown away".
You can see this e.g. on the jagged appearance of the
vertical wood pieces in the top right
part of the version that is scaled down by 2.
To fix this, you have to modify the program
so that each pixel of the new, smaller image is set to the
average of all pixels that are scaled down into that
pixel. E.g. assuming a scale factor of two,
the top left pixel of the new image is the average
of the four top left pixels in the old image. More precisely
a square area of two by two in the old image (where two is
the scale factor).
To achive this, do the following:
- First, review how to compute an average: You first have to initialize
a variable to zero, add up all items in that variable,
and finally divide it by the number of items.
- The area corresponding to a pixel in the new image is an
area of "scale factor" by "scale factor" pixels in the original image.
To compute an average over that, you will need two loops that
iterate over those pixels.
- You already know the top left pixel of that area! It is the pixel
that is read by the original code segment shown above.
Lab Questions and What to Turn in
As mentioned above, you have to add comments with
your own identification (name, student ID, date).
Remember to save the file after you modify it!
How to submit the lab
Exactly like in lab02, you hand in this lab the submit command on the Linux systems that are behind the PRISM lab infrastructure. For details on how to submit, please refer to lab02. However, this time please
submit to lab06, i.e. issue the command
submit 1710 lab06 lab06.py
Note: You must do all the above steps correctly for receiving
full credit for this lab.
Common Problems
All of the following mistakes led to various deductions
- Input errors were not caught: Division by 0 was not avoided
- Did not rename the function
- Did not run (unfinished program but effort is shown)
- If the code had small problems (e.g input errors were generally
handled but not perfectly) but a great effort was shown in de-pixelation
section, +0.25 was awarded.