Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imageio: How to increase quality of output gifs?

  • I have a list of .png (PIL) images I want to string together into an animated gif
  • To do this, I am currently using the imageio library
  • However, I cannot find a way to do this to produce good quality gifs

As far as I know, there are two types of settings I can change. The imageio.imread() settings for reading .pngs, and the imageio.mimwrite() settings for writing gifs. According to imageio.help(),

  1. imageio.imread() has one read paramter only for PNG-PIL, ignoregamma which takes a boolean. This does not change anything for my output gifs.
  2. imageio.mimwrite() can refer to two formats. The first is GIF-PIL. The output from this format only shows one frame, and is thus undesirable. Output here.
  3. imageio.mimwrite() has the second format GIF-FI. This produces more promising outputs with the following options:
    • 'quantizer':'wu' generates a full gif with specified frames and frame rates, but produces a 'corrupted' kind-of quality. Output here.
    • 'quantizer':'nq' generates a full gif with better 'less corrupted' quality than 'wu', but does not handle colour well. Notice how the legend in the bottom right tends to change its colour. Output here.

Here's the relevant code for the best quality I could get so far (GIF-FI with nq)

def gen_gif(self, datetime_list):
    kwargs_write = {'fps':5.0, 'quantizer':'nq'}
    frames = []
    for datetime in datetime_list:
        frames.append(imageio.imread(datetime+'.png'))
    exportname = '{} to {}.gif'.format(datetime_list[0], datetime_list[-1])
    imageio.mimsave(exportname, frames, 'GIF-FI', **kwargs_write)

The function is called with one list parameter of strings containing the full path to the .png images to be compiled to the gif.

like image 735
ning Avatar asked Dec 11 '16 09:12

ning


1 Answers

The GIF format can only handle 256 colors per frame. The quantizer parameter determines the method that will be used to find these colors.

The best way to control which colors will be used is probably to reduce the number of colors (to 256) on your own.

like image 191
Gwen Avatar answered Oct 05 '22 01:10

Gwen