3
votes

I need to make a function that will copy an image, but mirrored. I created the code to mirror the image, but it isn't working and I don't know why because I traced the code and it should be mirroring the image. Here's the code:

def invert(picture):
 width = getWidth(picture)
 height = getHeight(picture)

 for y in range(0, height):
   for x in range(0, width):
    sourcePixel = getPixel(picture, x, y)
    targetPixel = getPixel(picture, width - x - 1, height - y - 1)
    color = getColor(sourcePixel)
    setColor(sourcePixel, getColor(targetPixel))
    setColor(targetPixel, color)
 show(picture)
 return picture 

def main():
  file = pickAFile()
  picture = makePicture(file)
  newPicture = invert(picture)
  show(newPicture)

Can someone explain to me what is wrong? Thank you.

2
show(picture) and return picture should be indented - bozdoz
Please, update your code block to mirror ;-) what you have in your local file... - Gauthier Boaglio
@boazdoz when i do that it it gives me an error attempting to pass an argument to a function. - user2387191
@Golgauth i don't know what you mean...this course i'm takinh is jython for beginners so i haven't been introduced to any python APIs if that's what you're referring to. - user2387191
No, but fix your indentation in your question's body thus we don't get confused with bad indentation (click on edit link at the bottom) - Gauthier Boaglio

2 Answers

1
votes

Try this :

def flip_vert(picture):
    width = getWidth(picture)
    height = getHeight(picture)

    for y in range(0, height/2):
        for x in range(0, width):
            sourcePixel = getPixel(picture, x, y)
            targetPixel = getPixel(picture, x, height - y - 1)
            color = getColor(sourcePixel)
            setColor(sourcePixel, getColor(targetPixel))
            setColor(targetPixel, color)

    return picture 


def flip_horiz(picture):
    width = getWidth(picture)
    height = getHeight(picture)

    for y in range(0, height):
        for x in range(0, width/2):
            sourcePixel = getPixel(picture, x, y)
            targetPixel = getPixel(picture, width - x - 1, y)
            color = getColor(sourcePixel)
            setColor(sourcePixel, getColor(targetPixel))
            setColor(targetPixel, color)

    return picture 
1
votes

The problem is that you are looping across the whole image instead of only half of width. You mirror twice your image and get the same image as output as the one you input.

if you mirror across the Y axes the code should be

for y in range(0, height):
for x in range(0, int(width / 2)):