So I am trying to create a mirror image that is horizontal. I am having trouble getting the actual image to mirror because I am not sure how to go about doing this. I am using PIL 1.1.6 and Python 2.5.4, if that matters.

So far I have:

def mirrorHorizontal(y):
	horiz = y.copy()
	width1 = horiz.getWidth()
	height1 = horiz.getHeight()
	for row in range(height1):
	    for col in range(width1):
			
	return horiz

I do not know what to do with the rows and columns to get it to mirror in the middle horizontally. Anyone have any suggestions or pointers to get me going in the right direction?

Thanks.

Meant to include all the code:

from image import *

def mirrorHorizontal(x):
	horiz = x.copy()
	width1 = horiz.getWidth()
	height1 = horiz.getHeight()
	for row in range(height1):
	    for col in range(width1):
			
	return horiz

def main():
	myImage = FileImage("image.jpg")

	horiz = mirrorHorizontal(myImage)	

	myImage.show("Original", 100, 100)
	horiz.show("Mirrored horizontally", 400, 100)
	
	raw_input("Press Enter to exit")
	
main()

After screwing around with this code for an hour or so, I managed to get this:

def mirrorHorizontal(x):
	horiz = x.copy()
	width1 = horiz.getWidth()
	height1 = horiz.getHeight()
	middle = int(height1 / 2)
	for row in range(middle):
	    for col in range(width1):
			(r, g, b) = horiz.getPixel2D(col, row)
			horiz.setPixel2D(col, row - middle * -1, (r, g , b))
	return horiz

That gives me this image:

http://i44.tinypic.com/4vmvdk.jpg

I am so close! Does anyone have a suggestion to get the bottom half to flip over? Thanks!

I don't want to spoil your fun, but PIL has a flip method to create a mirror image ...

from PIL import Image

img = Image.open("image.jpg")

# flip the image around a vertical axis
# for horizontal axis use: Image.FLIP_TOP_BOTTOM
img_flipped = img.transpose(Image.FLIP_LEFT_RIGHT)

img_flipped.show()

I don't think the assignment let's us do that, but I will ask. If it isn't, how would I go about correcting my situation?

Thanks!

Edit: Tried your idea, but transpose doesn't seem to work.

I don't think the assignment let's us do that, but I will ask. If it isn't, how would I go about correcting my situation?

Thanks!

Edit: Tried your idea, but transpose doesn't seem to work.

I don't think you are using PIL. PIL would work this way ...

from PIL import Image

def mirror_image_h(img):
    """create a mirror image of img around a horizontal axis"""
    # use a copy of image to create the mirror image in
    mirror = img.copy()
    width, height = img.size
    for row in range(height):
        for col in range(width):
            (r, g, b) = img.getpixel((col, row))
            mirror.putpixel((col, height - row - 1), (r, g , b))
    return mirror

img = Image.open("image.jpg")

img_flipped = mirror_image_h(img)

img_flipped.show()

Thanks so much! Works beautifully.

We were given the image.py library to use for the assignment, so maybe certain things were taken out so we couldn't use them as short cuts. I dunno.

Thanks a bunch for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.