I'm creating a simple GIF animation using PIL:
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")
The problem is that whenever I use Draw.text, image's background is getting some kind of white noze:
I found some info that I have to use getpalette from the first frame and putpalette for all the other frames like this:
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)
But it just gives me: ValueError: illegal image mode
.
What's the reason of the noizy background and how can I fix it?
UPD I was able to fix the background by changing image mode to "P", but in this case my fonts became unreadable. These are examples with RGB mode (fonts are well) and P mode (fonts are awful):
Why am I getting either nice background or nice fonts but not both? Is there a workaround?