问题
- 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()
,
imageio.imread()
has one read paramter only forPNG-PIL
,ignoregamma
which takes a boolean. This does not change anything for my output gifs.imageio.mimwrite()
can refer to two formats. The first isGIF-PIL
. The output from this format only shows one frame, and is thus undesirable. Output here.imageio.mimwrite()
has the second formatGIF-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