You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
iflen(frames) >1:
# Generate an image with all frames, and crunch it to ADAPTIVE to form a master colour palettemaster=Image.new('RGB', (32, 24*len(frames)))
forindex, imageinenumerate(frames):
master.paste(image, (0, 24*index))
master=master.convert('P', dither=False, palette=Image.ADAPTIVE, colors=256)
forindex, imageinenumerate(frames):
image=image.convert('P', dither=False, palette=master.palette)
# image = image.quantize(method=3, palette=master)image=image.transpose(Image.ROTATE_270).transpose(Image.FLIP_LEFT_RIGHT)
image=image.resize(OUTPUT_SIZE, Image.NEAREST)
frames[index] =imagefilename='mlx90640-{}.gif'.format(
datetime.now().strftime("%Y-%m-%d-%H-%M-%S"))
print("Saving {} with {} frames.".format(filename, len(frames)))
frames[0].save(
filename,
save_all=True,
append_images=frames[1:],
duration=1000//fps,
loop=0,
include_color_table=True,
optimize=True,
palette=master.palette.getdata())
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?
OS: Linux, Raspbia
Python: 3.4.9
Pillow: PIL.PILLOW_VERSION == 5.4.1
The text was updated successfully, but these errors were encountered:
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.
fromPILimportImage, ImagePalette, ImageSequencewithImage.open("input.gif") asim:
# Construct a palette with all colors from the imagepalette=ImagePalette.ImagePalette()
forframeinImageSequence.Iterator(im):
fori, countinenumerate(frame.histogram()):
ifcount:
color=tuple(frame.palette.palette[i*3 : i*3+3])
palette.getcolor(color)
# Save, using that palette for all framesim.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: