Mozein
Mozein

Reputation: 807

Horizentally flip of an image [python]

so I need to flip an image horizontally, meaning in regards to the horizontal axes, like this picture enter image description here

however my problem is it's flipping in regards to the vertical axes, as shown in this picture enter image description here

This is my code right now. May someone explain the changes I need to do ? image.gif is the image I am trying to flip. the reason I am setting the newImage at a new position as shown below is because I want the image to come up in the same window as the old image. and it did! only flipped vertically not horizontally.

from cImage import*
def horizentalFlip(oldimage):
    myimagewindow = ImageWin("image",1000,600)
    oldimage = FileImage("image.gif")
    oldimage.draw(myimagewindow)
    oldw = oldimage.getWidth()
    oldh = oldimage.getHeight()

    newImage = EmptyImage(oldw,oldh)

    maxp = oldw - 1
    for row in range(oldh):
        for col in range(oldw):
            oldpixel = oldimage.getPixel(maxp-col,row)
            newImage.setPixel(col,row,oldpixel)
    newImage.setPosition(oldw+1,0)
    newImage.draw(myimagewindow)
    myimagewindow.exitOnClick()

Thank you

Upvotes: 4

Views: 5206

Answers (1)

jez
jez

Reputation: 15349

The type of flipping you want (reflection around the x axis, your first picture) is usually called a "vertical" flip, whereas reflection around the y axis (your second picture) is usually called a horizontal flip.

Nomenclature aside, you want reflection about the x axis and if I've understood you correctly your problem is that you're getting reflection about the y axis. This is because the following lines:

  maxp = oldw - 1
  ...
        oldpixel = oldimage.getPixel(maxp-col,row)

manipulate widths (w), columns (col) and the first coordinate argument of each getPixel call: all these concepts are relevant to manipulation of the horizontal coordinate of each pixel. But you want to change the vertical coordinate of each pixel, so you need to work with height, with rows, and with the second coordinate argument:

  maxp = oldh - 1
  ...
        oldpixel = oldimage.getPixel(col, maxp - row)

Upvotes: 3

Related Questions