imageio: How to increase quality of output gifs?

主宰稳场 提交于 2021-02-08 14:10:23

问题


  • 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.


回答1:


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.



来源:https://stackoverflow.com/questions/41084883/imageio-how-to-increase-quality-of-output-gifs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!