Question

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)
Was it helpful?

Solution

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.

OTHER TIPS

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:

from PIL 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')

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...

Here's how to mirror diagonally in JES; It only works for a square image though:

def mirrorDiagonal(picture):
  for sourceX in range(0,getWidth(picture)):
    for sourceY in range (0,getHeight(picture)):
      pex=getPixel(picture,sourceY,sourceX) 
      pix=getPixel(picture, sourceX,sourceY)
      color=getColor(pix)
      setColor(pex,color)
  show(picture)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top