views:

750

answers:

3

Hey

I've been making a picture mirroring in horizontal and vertical axes. Now I'm going to make the diagonal.

I had done the hori and verti width two for loops which in the hori scenario loops through all the pixels in the height and only the half of the pixels in the width. Then it gets the color of the pixel and set the same color to the pixel on the other side. Going from the getWidth(pic) to the center.

Then I have my mirror in the middle of the pic. How to do the diagonal way?

Edit:

img_src = makePicture(pickAFile())
W = getWidth(img_src)
H = getHeight(img_src)

for x in range(W):
        for y in range(H):
                p = getPixel(img_src, x, y)
                colorInSrc = getColor( getPixel(img_src, x, y) )
                destPixel = getPixel(img_src, H-y-1, W-x-1)
                setColor(destPixel, colorInSrc)
+1  A: 

It's not really an Python question, is it?

The easiest solution would be to first mirror horizontal and then vertical. Another one would be to switch pixel rows with columns.

Or to do your algorithm but switch the pixels from left-top to bottom-right...

Sven Hecht
quote Sven. The simpliest way is to mirror along horizontal dim and than vertical.
DrFalk3n
+1  A: 

If I understood correctly what you need is to "flip" the image by a diagonal. Since there are two of them I'll presume that you mean the one that goes from left bottom to right top.

In order to flip by this diagonal you need to transform each row from the source in columns in the destination. The left part of the rows will become the bottom part of the new columns. Also the topmost row will become the rightmost column. You will need to do this pixel by pixel on the whole image. Also keep in mind that the width and height of the image will be swapped.

Edit: A small example. Say you start with an image 5 pixels wide and 3 pixels high (5x3). You will need to create a new blank image 3 pixels wide and 5 pixels high.

If you start pixel numbering from left top corner with (0,0), then this pixel will end up at (2,4) in the new image, pixel (1,0) will end at (2,3) and so on.

If your original width and height are W and H then you should use something like this:

for x in xrange(W):
 for y in xrange(H):
  p = img_src.getpixel(x, y)
  img_dest.setpixel(H-y-1, W-x-1)

This should work, but is not tested.

rslite
I see, but im still uncertain on the way the program should be build.I need to for-loops?for x in range(0,getWidth(pic): for x in range(0,getHeight(pic):Then how am i going to flip the pixels? It dosnt matter wether ill flit in the diagonal that goes from upper left to bottom right, or bottom left to upper right.
designer
You need a new image in which to set the pixels. See my edit shortly.
rslite
Thanks, ive pastet my code now. But not working. Is it because of non square images?
designer
+1  A: 

Using PIL (the Python Imaging Library) this is a relatively straightforward task. Notice however, that the output image is square -- thus not the same size as the original image.

Here is the code:

import Image, ImageDraw

# load the image, create the mirrored image, and the result placeholder
img    = Image.open('img.png')
mirror = img.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_90)
sz     = max(img.size + mirror.size)
result = Image.new(img.mode, (sz,sz))
result.paste(img, (0,0)+img.size)

# now paste the mirrored image, but with a triangular binary mask
mask = Image.new('1', mirror.size)
draw = ImageDraw.Draw(mask)
draw.polygon([0,0,0,sz,sz,sz], outline='white', fill='white')
result.paste(mirror, (0,0)+mirror.size, mask)

# clean up and save the result
del mirror, mask, draw
result.save('result.png')
Paul
Thanks but im only using JES. Its a program for students aka noobs... so your version is probably making sense for others :)
designer