0

I'm trying to mirror an image. That is, if, e.g., a person is facing to the left, when the program terminates I want that person to now be facing instead to the right.

I understand how mirroring works in JES, but I'm unsure how to proceed here. Below is what I'm trying; be aware that image is a global variable declared in another function.

def flipPic(image):
  width = getWidth(image)
  height = getHeight(image)
  for y in range(0, height):
    for x in range(0, width):
       left = getPixel(image, x, y)
       right = getPixel(image, width-x-1, y)
       color = getColor(left)
       setColor(right, color)
  show(image)
  return image
Søren Debois
  • 5,598
  • 26
  • 48

3 Answers3

0

try this

width = getWidth(pic)
height = getHeight(pic)
for y in range (0,height):
    for x in range (0, width/2):
        left=getPixel(pic, x, y)
        right=getPixel(pic, width-x-1,y)
        color1=getColor(left)
        color2=getColor(right)
        setColor(right, color1)
        setColor(left, color2)
repaint(pic) 
mhlester
  • 22,781
  • 10
  • 52
  • 75
  • Hi, I fixed the formatting for your code. Make sure all lines are indented four spaces for Stack Overflow to format properly. You can also select the code and press the `{}` button on the toolbar to do that automatically. – mhlester Apr 08 '14 at 23:27
0

I personally find that repaint is confusing for newbies (like me!). I'd suggest something like this:

def mirrorImage(image):

width = getWidth(image)
height = getHeight(image)
for y in range (0,height):
    for x in range (0, width/2):
        left=getPixel(pic, x, y)
        right=getPixel(pic, width-x-1,y)
        color1=getColor(left)
        color2=getColor(right)
        setColor(right, color1)
        setColor(left, color2)
        show(image)
        return image
mirrorImage(image)
jams
  • 39
  • 1
  • 1
  • 10
0

This seems to work well.. I put some comments in so you can rewrite in your own style.

feel free to ask questions but I think your question may already be answered^^

#this function will take the pixel values for a selected picture and 
#past them to a new canvas but fliped over! 
def flipPic(pict):
#here we take the height and width of the original picture
  width=getWidth(pict)
  height=getHeight(pict)
#here we make and empty canvas  
  newPict=makeEmptyPicture(width,height) 
#the Y for loop is setting the range to working for the y axes the started the X for loop 
  for y in range(0, height):
#the X for loop is setting the range to work in for the x axis 
    for x in range(0, width):
#here we are collecting the colour information for the origional pix in range of X and 
      colour=getColor(getPixel(pict,x,y))
#here we are setting the colour information to its new position on the blank canvas
      setColor(getPixel(newPict,width-x-1,y),colour)
      #setColor(getPixel(newPict,width-x-1,height-y-1),colour)#upsidedown
  show(newPict)

#drive function
pict = makePicture(pickAFile())
show(pict)
flipPic(pict)

Might be easier to read if you copy it over to JES first :D

BTW I got full marks for this one in my intro to programming class ;)