pdf to gif conversion and optimization
There have been several occasions that I need to generate some gif animations from a bunch of pdf plots (generated by gnuplot’s tikz terminal), so the following procedure merits documenting. Note that convert is part of ImageMagick, and gifsicle is a gif optimization tool.
- Converting pdfs to png:
for f in *pdf; do convert -verbose -density 150x150 $f `basename $f .pdf`.png; done - combining png to gif (and optimize):
convert -verbose +dither -layers Optimize -colors 32 -delay 5 -loop 0 -dispose previous *png output.gif - if there are too many *png files, it’s probably better to divide and conquerer: assuming png files are frame_000 upto frame_139, then first:
for i in `seq -f "%02g" 0 13`; do convert -verbose +dither -layer Optimize -delay 5 -loop 0 -dispose previous frame_${f}?.png frame_${f}.gif; done
then
convert -verbose frame_??.gif all.gif - notice that I’ve dropped the -color 32, b/c in my case it actually increases the resulting file size.
- further optimization (YMMV) using gifsicle:
gifsicle -O3 output.gif -o output_optimized.gif
cf:
p.s., a related operation is to combine say every 10th of the pdfs for closeup inspection:
pdfjam frame_??0.pdf --papersize '{12in,9in}' --outfile frames.pdf
pdfjam is a frontend to the latex package "pdfpages". The --papersize I used is the same as the one I had used in gnuplot when generating these plots (set term tikz size 12in,9in ...).
Leave a comment