簡體   English   中英

PIL:在圖像上繪制透明文本

[英]PIL: draw transparent text on top of an image

在我的項目中,我試圖在一些文本上創建一個大綱。 總體思路是偏移原始白色文本略微透明的黑色文本。

由於某些原因,我得到了黑色文本,但是透明的是幽冥。 這是MCVE:

image = Image.open("spongebob.gif").convert("RGBA")

draw = ImageDraw.Draw(image, "RGBA")
font = ImageFont.truetype("impact.ttf", 25)
draw.text((0, 0), "This text should be 5% alpha", fill=(0, 0, 0, 15), font=font)
image.save("foo.gif")

結果:

海綿鮑勃不高興

我錯過了什么?

這應該適合你。

from PIL import Image, ImageDraw, ImageFont

image = Image.open("spongebob.gif").convert("RGBA")
txt = Image.new('RGBA', image.size, (255,255,255,0))

font = ImageFont.truetype("impact.ttf", 25)
d = ImageDraw.Draw(txt)    

d.text((0, 0), "This text should be 5% alpha", fill=(0, 0, 0, 15), font=font)
combined = Image.alpha_composite(image, txt)    

combined.save("foo.gif")

編輯:Pillow文檔中的參考示例http://pillow.readthedocs.io/en/4.2.x/reference/ImageDraw.html#example-draw-partial-opacity-text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM