3

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.

Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85
user2387191
  • 65
  • 1
  • 4
  • 9
  • `show(picture)` and `return picture` should be indented – bozdoz Jun 16 '13 at 00:35
  • Please, update your code block to `mirror` ;-) what you have in your local file... – Gauthier Boaglio Jun 16 '13 at 00:39
  • @boazdoz when i do that it it gives me an error attempting to pass an argument to a function. – user2387191 Jun 16 '13 at 00:43
  • @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 Jun 16 '13 at 00:43
  • 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 Jun 16 '13 at 00:52
  • @Golgauth my bad, fixed! But yeah. Another weird thing is that when i divide height by 2 in the for y range, it flips the picture horizontally and vertically, but when I divide width by 2 it does the same thing. I think that in targetPixel if I make it just height instead of height - y - 1 it would work but when I do that my ide says there is an error. – user2387191 Jun 16 '13 at 00:57
  • @Golgauth an error of passing an argument to a function – user2387191 Jun 16 '13 at 01:07

2 Answers2

1

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 
Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85
  • that's so weird because I just came up with that code 10 minutes ago haha. But it still flips the picture vertically, just not horizontally. I tried fiddling around with it but it didn't produce the horizontal flip. – user2387191 Jun 16 '13 at 02:09
  • ok well i figured out the answer, for x in range should be width / 2 and the targetPixel should have width - x - 1, y thank you for your help guys! – user2387191 Jun 16 '13 at 02:23
1

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)):
Alexandru Barbarosie
  • 2,952
  • 3
  • 24
  • 46
  • so I would need to change for x in range(0, width): to for x in range(0, width / 2): ? if that's the case, when I try it, it flips the picture horizontally and vertically. I only want to flip it horizontally – user2387191 Jun 16 '13 at 01:11
  • 1
    If you want to flip if horizontaly it should go across half of the height. – Alexandru Barbarosie Jun 16 '13 at 01:14
  • Yeah, but even if I do that it gives me the same output of what would happen if i divided the width by 2. – user2387191 Jun 16 '13 at 01:28
  • Okay, so if I don't divide either range by two, it will just spit back the same image I input. If I divide the height by two in the y range, it flips the picture both horizontally and vertically. If I divide the width by 2, it does the same thing. I want it so that the picture only flips horizontally. – user2387191 Jun 16 '13 at 01:36
  • @user2387191 Made an update to my answer. What does that give ? – Gauthier Boaglio Jun 16 '13 at 01:58