Начиная с этого изображения (dog.jpg
):
![enter image description here](https://i.stack.imgur.com/fRprW.jpg)
Вы можете сделать что-то вроде этого:
#!/usr/bin/env python3
from PIL import Image, ImageDraw
# Open image, generate a copy and rotate copy 180 degrees
im1 = Image.open('dog.jpg')
im2 = im1.copy().rotate(180)
# DEBUG: im2.save('im2.png')
# Get dimensions of image
w, h = im1.size
# Create an alpha layer same size as our image filled with black
alpha = Image.new('L',(w,h))
# Draw 2 white (i.e. transparent) triangles on the alpha layer
draw = ImageDraw.Draw(alpha)
draw.polygon([(0, 0), (w, 0), (w/2, h/2)], fill = (255))
draw.polygon([(0, h), (w, h), (w/2, h/2)], fill = (255))
# DEBUG: alpha.save('alpha.png')
# Composite rotated image over initial image while respecting alpha
result = Image.composite(im1, im2, alpha)
result.save('result.png')
![enter image description here](https://i.stack.imgur.com/4JvTj.png)
Промежуточные шаги (прокомментированные с #DEBUG:
в коде) выглядят следующим образом:
im2.png
![enter image description here](https://i.stack.imgur.com/ERIlb.jpg)
и alpha.png
:
![enter image description here](https://i.stack.imgur.com/Oiung.png)