Tag Archives: imagemagick

Converting a PDF to JPG and back again, without looking terrible

Sometimes you need to convert a pdf to jpg, mess around with it, then recreate a pdf. Here’s the method I use:


convert -density 400 file.pdf -alpha remove file.jpg # this create file-0.jpg, file-1.jpg, etc. Between 300-600 is a good density, alpha remove is needed if there is transparency in the pdf
gimp file-0.jpg
convert -units PixelsPerInch -density 400 -quality 25 $(ls -v file-*.jpg) file.pdf # gimp usually uses a dpi of 72, so need to change resolution back to 400, a high quality = large file

That’s it! Almost. If there’s transparency in the pdf, jpg won’t like it so you’ll need to use png. But PNG doesn’t automatically add numbers, so you need to do:


convert -density 400 ~/Downloads/PBH.PDF pbh-%d.png

Sometimes you need to convert the pngs to jpg before making a pdf. YMMV.

Exploding, compositing, reanimating gifs


# -coalesce makes sure that each individual frame is complete
convert animated.gif -coalesce frame%05d.png

# compose and flatten frames
for x in frame*.png; do convert -page +0+0 base.png -page +200+10 $x -flatten +repage comp-`basename $x`; done

# get frame delays
identify -verbose animated.gif |grep Delay|sed 's/.* \([0-9]*\)x.*/\1/' > delays

# generate convert command from the delays
$( echo -n convert; i=0; for d in `cat delays`; do printf " -delay $d comp-frame000%02d.png " $i; let i++; done; echo " out.gif" )