簡體   English   中英

使用PIL Python向圖像添加文本后如何保留圖像的原始顏色?

[英]How to preserve the original colors of an image after adding text to it using PIL Python?

我正在嘗試使用 PIL 在我的圖像上添加一些文本,請參閱下面的代碼,

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw 
import sys

image = Image.open('image.png')
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial',40)
draw.text((700, 470),'Text',(0,0,0),font=font)
img.save('out-image.png','PNG')

但我失去了圖像的原始顏色,見下圖,

原圖

添加文字后

我如何才能保留原始顏色。

謝謝你

對我來說,這看起來像是 PIL 中的一個錯誤。 我認為這是因為您的圖像已調色並且draw.text()弄亂了調色板。

要解決此問題,您可以在打開時轉換為 RGB 圖像以避免調色板問題。 改成這樣:

image = Image.open('image.png').convert('RGB')

暫無
暫無

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

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