这是一种解决方法:
#!/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')
Image.fromarray(npImage).save('result.png')
和draw.pieslice([0,0,h,w],0,360,fill=255)
中,我遇到了错误Cannot handle this data type: (1, 1, 5), |u1
。我不得不通过draw.pieslice(((0, 0), (h, w)), 0, 360, fill=255)
进行更新。 - alambertt