正如標題所說。如何在 Python 中使用 Pillow 繪制下圖的白色部分?假設背景可以是任何東西,并且在我撰寫程式時不知道(但可能不是統一的黑色,可能根本不統一)。

的檔案ImageDraw確實具有與pieslice我想要的完全相反的功能。并且檔案ImagePath根本沒有提到弧。
uj5u.com熱心網友回復:
使用方法Image.composite合成兩張圖片,一張是您的源影像,另一張繪制倒置的餅圖,其中只有alpha = 1的白色區域,其他區域的alpha = 0。
from PIL import Image, ImageDraw
def pieslice(im, w1, w2, fill="#ffffffff"):
# Create a all transparent image
im2 = Image.new('RGBA', (w1, w1), color="#00000000")
draw = ImageDraw.Draw(im2, mode="RGBA")
d = (w1 - w2) // 2
# Draw a nontransparent box
draw.rectangle([(d, d), (w2 d - 1, w2 d - 1)], fill=fill)
# Draw a transparent pie slice
draw.pieslice([(d, d), (2 * w2 d - 1, 2 * w2 d - 1)], 180, 270, fill="#00000000")
# Get alpha layer as mask reference in method composite
alpha = im2.getchannel("A")
new_im = Image.composite(im2, im, alpha)
return new_im
w1, w2 = 200, 180
# Can use any existing square image
im = Image.new('RGBA', (w1, w1), color="#000000ff")
new_im = pieslice(im, w1, w2)
new_im.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/372830.html
