Вырезать треугольники и смешать их - PullRequest
0 голосов
/ 23 января 2019

Здравствуйте, не могли бы вы дать мне какие-нибудь советы о том, как вырезать 4 треугольника из изображения и смешать их вместе, чтобы они выглядели так:

enter image description here

Я хочу использовать питона для достижения этой цели.

1 Ответ

0 голосов
/ 27 января 2019

Начиная с этого изображения (dog.jpg):

enter image description here

Вы можете сделать что-то вроде этого:

#!/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

Промежуточные шаги (прокомментированные с #DEBUG: в коде) выглядят следующим образом:

im2.png

enter image description here

и alpha.png:

enter image description here

...