【发布时间】:2021-03-06 08:39:10
【问题描述】:
我正在使用 PIL 创建一个简单的 GIF 动画:
from PIL import Image, ImageDraw, ImageFont
images = []
for x, i in enumerate(range(10)):
image = Image.new(mode="RGB", size=(320, 60), color="orange")
draw = ImageDraw.Draw(image)
fnt = ImageFont.truetype('font.ttf', size=10)
draw.text((10, 10), ("%s" % x), fill=(0, 0, 255), font=fnt)
images.append(image)
images[0].save("result/pil.gif", save_all=True, append_images=images[1:], duration=1000, loop=0, format="GIF")
问题是每当我使用 Draw.text 时,图像的背景都会出现某种白色的鼻子:
我发现了一些信息,我必须从第一帧使用 getpalette 并为所有其他帧使用 putpalette,如下所示:
for x, i in enumerate(range(10)):
image = Image.new(mode="RGB", size=(320, 60), color="orange")
if x == 0:
palette = image.getpalette()
else:
image.putpalette(palette)
但它只给了我:ValueError: illegal image mode。
背景嘈杂的原因是什么,我该如何解决?
UPD 我可以通过将图像模式更改为“P”来修复背景,但在这种情况下,我的字体变得不可读。这些是 RGB 模式(字体很好)和 P 模式(字体很糟糕)的示例:
为什么我会得到漂亮的背景或漂亮的字体,但不能两者兼得?有解决办法吗?
【问题讨论】:
-
如果可以选择,请考虑使用 .apng 或 .webp 格式
标签: python python-imaging-library gif animated-gif