如何在使用PIL Python向图像添加文本后保留图像的原始颜色?
我正在尝试使用PIL在我的图像上添加一些文本,请参见下面的代码如何在使用PIL Python向图像添加文本后保留图像的原始颜色?,python,image,text,python-imaging-library,Python,Image,Text,Python Imaging Library,我正在尝试使用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
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中的一个bug。我认为这是因为您的图像被调色板化,而
draw.text()
正在弄乱调色板
对于解决方案,可以在打开RGB图像时将其转换为RGB图像,以避免调色板问题。更改为:
image = Image.open('image.png').convert('RGB')