ddbg
ddbg

Reputation: 37

Python Image Reverse

I am trying to flip an image to the complete opposite (left to right) and cannot seem to figure out the code. I know how to mirror them half and such, however the complete flip seems to be eluding me. I have this so far:

def flip(source):
  width=getWidth(source)/2
  height=getHeight(source)
  for y in range(0,height):
    for x in range(0,width):
      leftPixel=getPixel(source,x,y)
      rightPixel=getPixel(source,width-x-1,y)
      color1=getColor(leftPixel)
      color2=getColor(rightPixel)
      setColor(rightPixel,color1)
      setColor(leftPixel,color2)

Upvotes: 0

Views: 625

Answers (3)

soupault
soupault

Reputation: 6520

It's a nice style to use already implemented functions:

flip left <-> right

flip up <-> down

Upvotes: 0

msw
msw

Reputation: 43527

Your code will take a very long time to run relative to the ImageOps.mirror() function of the Python Imaging Library.

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 362147

rightPixel=getPixel(source,width-x-1,y)

In this line, width should be the full width of the image, not half the width. I recommend moving the /2 into the inner loop range.

width  = getWidth (source)
height = getHeight(source)

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

Upvotes: 1

Related Questions