Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flipping a image vertically, relation ship between the original picture and the new one. [python]

Tags:

python

image

I am trying to flip a picture on its vertical axis, I am doing this in python, and using the Media module.

like this: enter image description here

i try to find the relationship between the original and the flipped. since i can't go to negative coordinates in python, what i decided to do is use the middle of the picture as the reference.

so i split the picture in half,and this is what i am going to do:

[note i create a new blank picture and copy each (x,y) pixel to the corresponding to (-x,y), if the original pixel is after the middle.

if its before the middle, i copy the pixel (-x,y) to (x,y)

enter image description here

so i coded it in python, and this is the result.

Original: enter image description here

i got this:

import media

pic=media.load_picture(media.choose_file())


height=media.get_height(pic)
width=media.get_width(pic)
new_pic=media.create_picture(width,height)

for pixel in pic:
   x_org=media.get_x(pixel)
   y_org=media.get_y(pixel)
   colour=media.get_color(pixel)
   new_pixel_0=media.get_pixel(new_pic,x_org+mid_width,y_org) #replace with suggested     
                                                              #answer below
   media.set_color( new_pixel_0,colour)


media.show(new_pic)

enter image description here

this is not what i wanted, but i am so confused, i try to find the relationship between the original pixel location and its transformed (x,y)->(-x,y). but i think that's wrong. If anyone could help me with this method it would be great full.

at the end of the day i want a picture like this:

enter image description here

http://www.misterteacher.com/alphabetgeometry/transformations.html#Flip

like image 260
Wobblester Avatar asked Nov 26 '22 22:11

Wobblester


2 Answers

Why not just use Python Imaging Library? Flipping an image horizontally is a one-liner, and much faster to boot.

from PIL import Image
img = Image.open("AFLAC.jpg").transpose(Image.FLIP_LEFT_RIGHT)
like image 74
kindall Avatar answered Nov 29 '22 12:11

kindall


Your arithmetic is incorrect. Try this instead...

new_pixel_0 = media.get_pixel(new_pic, width - x_org, y_org)

There is no need to treat the two halves of the image separately.

This is essentially negating the x-co-ordinate, as your first diagram illustrates, but then slides (or translates) the flipped image by width pixels to the right to put it back in the range (0 - width).

like image 41
Johnsyweb Avatar answered Nov 29 '22 13:11

Johnsyweb