John Smith
John Smith

Reputation: 1099

PIL mirror image coordinates

I have a image that I created using PIL

import Image
import ImageDraw

img = Image.new("RGB", (400,400), "white")
draw = ImageDraw.Draw(img)

coords = [(100,70), (220, 310), (200,200)]
dotSize = 2

for (x,y) in coords:
    draw.rectangle([x,y,x+dotSize-1,y+dotSize-1], fill="black")

I know want to edit this image and take each coordinate and make a diagonal mirror image of it, on the same image.

Is there a method I can use for this? I would like to have this effect !https://i.sstatic.net/hLL15.jpg

Upvotes: 2

Views: 2922

Answers (1)

Junuxx
Junuxx

Reputation: 14251

You can use PIL's transpose, rotate and composite functions to achieve the desired result. These are all in the Image module.

I changed initial image a bit to make the result clearer. For starters, I increased the size of each dot to make them more noticeable.

The code below first shows the initial image overlaid with the mask that selects the half of the image below the top-left-to-bottom-right diagonal.

enter image description here

Then it shows the composite of the original and mirror images.

enter image description here

import Image
import ImageDraw

imsize = 400
img = Image.new("L", (imsize,imsize), "white")
draw = ImageDraw.Draw(img)

coords = [(100,70), (220, 310), (200,200), (80,20)]
dotSize = 50

for (x,y) in coords:
    draw.rectangle([x,y,x+dotSize-1,y+dotSize-1], fill="black")

## mirror image along the diagonal
img2 = img.rotate(90).transpose(Image.FLIP_TOP_BOTTOM)

## mask
mask = Image.new("L", (imsize,imsize), "black")
maskdraw = ImageDraw.Draw(mask)
# draw a triangle on the mask dividing the image along the diagonal
maskdraw.polygon([(0,0),(0,imsize),(imsize,imsize)], fill="white")

# show the mask overlaid on the original image
Image.blend(mask, img, 0.5).show()

# compute and show the blended result
img3 = Image.composite(img, img2, mask)
img3.show()

Upvotes: 3

Related Questions