怎么用Python把一个图片裁剪成心形

怎么用Python把一个图片裁剪成心形


回答:

python">import numpy as np

from PIL import Image, ImageDraw

def heart(size):

width, height = size

img = Image.new('L', size, 0)

draw = ImageDraw.Draw(img)

polygon = [

(width / 10, height / 3),

(width / 10, 81 * height / 120),

(width / 2, height),

(width - width / 10, 81 * height / 120),

(width - width / 10, height / 3),

]

draw.polygon(polygon, fill=255)

draw.ellipse((0, 0, width / 2, 3 * height / 4), fill=255)

draw.ellipse((width / 2, 0, width, 3 * height / 4), fill=255)

return img

img = Image.open("woman.jpg").convert("RGB")

npImage = np.array(img)

img_heart = heart(img.size)

npAlpha = np.array(img_heart)

npImage = np.dstack((npImage, npAlpha))

Image.fromarray(npImage).save('result.jpg')

原理是这么个原理啊,具体数值你自己再调整一下。

以上是 怎么用Python把一个图片裁剪成心形 的全部内容, 来源链接: utcz.com/a/164553.html

回到顶部