我想创建一个脚本,以圆形的方式裁剪图像。
我有一个服务器,接收所有种类的图片(所有相同的大小),我希望服务器裁剪收到的图像。
例如,将此图像转换为:
如下所示:
我希望能够将其保存为PNG (具有透明背景)。
如何做到这一点?
发布于 2018-07-24 05:01:53
这里有一种方法:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image, ImageDraw
# Open the input image as numpy array, convert to RGB
img=Image.open("dog.jpg").convert("RGB")
npImage=np.array(img)
h,w=img.size
# Create same size alpha layer with circle
alpha = Image.new('L', img.size,0)
draw = ImageDraw.Draw(alpha)
draw.pieslice([0,0,h,w],0,360,fill=255)
# Convert alpha Image to numpy array
npAlpha=np.array(alpha)
# Add alpha layer to RGB
npImage=np.dstack((npImage,npAlpha))
# Save with alpha
Image.fromarray(npImage).save('result.png')
https://stackoverflow.com/questions/51486297
复制相似问题