1 个回答
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.png')

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题