-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GIF - use single colour table for all frames #3788
Comments
Taking your 1.2mb image, and running from PIL import Image
im = Image.open('input.gif')
im.save('output.gif', save_all=True) Before #5291, the output is 1.3mb. After, it is 320kb. So we're not using a single color table yet, but that is a step towards solving your underlying problem. |
I've created #5603 to resolve this. With it, this code produces a 281kb file, down from the 320kb file from the code in my last post. from PIL import Image, ImagePalette, ImageSequence
with Image.open("input.gif") as im:
# Construct a palette with all colors from the image
palette = ImagePalette.ImagePalette()
for frame in ImageSequence.Iterator(im):
for i, count in enumerate(frame.histogram()):
if count:
color = tuple(frame.palette.palette[i * 3 : i * 3 + 3])
palette.getcolor(color)
# Save, using that palette for all frames
im.save("output.gif", save_all=True, palette=palette.palette) |
That's mighty close to the output I got above, and a considerable size reduction! Amazing. Thank you for taking the time to do this- it would have taken me forever just to figure out how to figure out how. |
What did you do?
Attempted to normalise a sequence of
P
images to use the same colour palette in an attempt to output a smaller GIF file that uses a common palette across all frames.The complete code is here - https://github.com/pimoroni/mlx90640-library/blob/master/python/rgb-to-gif.py - but the relevant snippet can be found below:
What did you expect to happen?
For the GIF format save method to understand that I was sharing a colour palette across frames, and export accordingly. There doesn't seem to be a way - that I can identify - to communicate this.
What actually happened?
Result a ~1.19MiB GIF:
Versus a ~263.4KiB GIF - This is a ~70-80% size saving just running the result through ezgif (https://ezgif.com/optimize) optimisation and picking "Use single color table for all frames":
Unless I am very much misunderstanding what ezgif is doing in this case, no other optimisation was performed.
What are your OS, Python and Pillow versions?
The text was updated successfully, but these errors were encountered: