CSE 1710.03A Programming for Digital Media

Lab 07

Due date: Nov 02, 2009 at 20:00

An(other) way to "flip" an image across the diagonal

A simple kind of image manipulation is to flip an image across the diagonal. The following 2 pictures illustrate this effect. Note that this result cannot be achieved by rotating the image.

For simplicity, the following instructions assume that the input is a square image, i.e. an image where width and height are equal!

  1. Copy and paste the Jython code below into a new JES window.
    # Flip Image (doesn't work)
    def flipimage(filename):
      pic = makePicture(filename)
      targetX = 0
      for sourceX in range(0,getWidth(pic)):
        targetY = 0
        for sourceY in range(0,getHeight(pic)):
          color = getColor(getPixel(pic,sourceX,sourceY))
          setColor(getPixel(pic,targetY,targetX),color)
          targetY = targetY + 1
        targetX = targetX + 1
      show(pic)
      return pic
    
  2. Before you proceed, add comment lines at the beginning of the code with your name, the date, and your student number (as in previous labs).
  3. Now you save this file, as lab07.py, and load it. Try it on the following imag, by first saving the first image to a local file called "barb2.jpg". Then call flipimage() with an appropriate filename as argument, e.g. r"Z:\barb2.jpg", or use filename = pickAFile() in the command line and then call flipimage(filename).
  4. As you can see, the image gets destroyed by this segment of code. Add a comment at the beginning of the code to explain why the image gets destroyed. Hint: consider in which order the pixels are copied. Another hint: consider e.g. what happens for the second column of pixels, after the first has already been copied!
  5. To fix the issue, one option is to copy to a new image, but this will be covered in class. Instead of copying pixels, another idea is to swap pairs of pixels. To swap two variables, a and b, the following code segement can be used:
      temp = a
      a = b
      b = temp
    
    Adapt this idea to fix the above code. Hint: instead of assignments, use appropriate get/set functions.
  6. If you try this now again, you will see that the picture now is undamaged. However, the program does not seem to do anything to the picture anymore. Add a second comment at the beginning of lab07.py to explain why swapping the pixels doesn't work. Hint: consider how often any given pixel is swapped.
  7. As the last step consider limiting the loop iterations to the triangular bottom left half of the image. Then the picture will be flipped correctly along the diagonal. Hint: To achieve this, modify the loop statement for the vertical iteration. Instead of iterating to the edge of the image in the inner loop, iterate only to the diagonal, where x = y.
  8. Make sure to test lab07.py again, before you proceed to the next step.

Lab Questions and What to Turn in

As mentioned above, you have to add comments with your own information, the explanation why the original code is broken, and why first fix didn't work as intended. 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 lab07, i.e. issue the command

submit 1710 lab07 lab07.py