Page 1 of 1

Convert multipage pdf to animated gif (macports)

Posted: 2013-03-20T10:45:05-07:00
by leingang
I have a two-page PDF file that I would like to convert to an animated GIF file. A post on the TeX StackExchange (http://tex.stackexchange.com/a/23728/1402) gives the following ImageMagick one-liner:

Code: Select all

$ convert -verbose -delay 50 -loop 0 -density 300 file.pdf file.gif
If I try this on a linux box (convert --version gives 6.5.4-7 2012-04-10) I get this log:

Code: Select all

"gs" -q -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 "-sDEVICE=pnmraw" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r300x300"  "-sOutputFile=/tmp/magick-XX30a4nG" "-f/tmp/magick-XX690OkA" "-f/tmp/magick-XX3GCKhu"
    /tmp/magick-XX30a4nG[0] PNM 1512x1134 1512x1134+0+0 8-bit DirectClass 9.811mb
    /tmp/magick-XX30a4nG[1] PNM 1512x1134 1512x1134+0+0 8-bit DirectClass 9.811mb
    file.pdf[0] PDF 1512x1134 1512x1134+0+0 16-bit DirectClass 9.811mb
    file.pdf[0] PDF 1512x1134 1512x1134+0+0 16-bit DirectClass 9.811mb
    file.pdf=>file-linux.gif[0] PDF 1512x1134 1512x1134+0+0 16-bit Palette PseudoClass 111c 2.620u 0:03
[gs is GPL Ghostscript 8.70 (2009-07-31)] and it works as expected. But if I try this on my mac, using the ImageMagick suite installed by macports (6.8.0-7 2013-01-04), I get this log:

Code: Select all

    "/opt/local/bin/gsx" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r300x300"  "-sOutputFile=/var/folders/kf/t7750q1w8xjcjy0059bpyx180000gq/T/magick-15750GLLaATgky5I1" "-f/var/folders/kf/t7750q1w8xjcjy0059bpyx180000gq/T/magick-15750ljasKifrwJcW" "-f/var/folders/kf/t7750q1w8xjcjy0059bpyx180000gq/T/magick-15750w8mj9AZrkJA8"
    /var/folders/kf/t7750q1w8xjcjy0059bpyx180000gq/T/magick-15750GLLaATgky5I1 PNG 1512x1134 1512x1134+0+0 8-bit sRGB 64.5KB 0.050u 0:00.049
    file.pdf PDF 1512x1134 1512x1134+0+0 16-bit sRGB 64.5KB 0.000u 0:00.000
    file.pdf=>file.gif PDF 1512x1134 1512x1134+0+0 16-bit Palette sRGB 106c 0.410u 0:00.409
[/opt/local/bin/gsx is GPL GhostScript 9.06 (2012-08-08)]
This time the resulting GIF file has only one frame, created from the first page of the PDF.

My workaround so far has been to use pdftk to split out each page of the PDF to its own file, then convert to merge each of the individual files into one GIF.

Code: Select all

$ pdftk file.pdf burst
$ convert [options] pg_*.pdf file.gif
This works fine, but I'd prefer a one-line script.

The mac versions being more recent, I would guess it's a matter of implementation. The only major difference I see between the two is -sDEVICE=pnmraw for the linux version and -sDEVICE=pngalpha for the mac. Can I tell convert to pass this option to gsx?

Re: Convert multipage pdf to animated gif (macports)

Posted: 2013-03-20T13:06:17-07:00
by snibgo
You can edit delegates.xml in your IM installation. I don't know enough about gs to suggest what the settings should be, but changing the working version to match the other will probably do the trick.

Re: Convert multipage pdf to animated gif (macports)

Posted: 2013-03-20T13:37:15-07:00
by leingang
snibgo wrote:You can edit delegates.xml in your IM installation. I don't know enough about gs to suggest what the settings should be, but changing the working version to match the other will probably do the trick.
It doesn't seem as easy as that. Both the linux and mac delegates.xml have lines that read

Code: Select all

  <delegate decode="ps:color" stealth="True" command=""gs" -q -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 "-sDEVICE=pnmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>
But the mac delegates.xml has an additional line that reads

Code: Select all

  <delegate decode="ps:alpha" stealth="True" command=""/opt/local/bin/gsx" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>
So unless I misunderstand convert looks for ps:color on linux and ps:alpha on mac.

Assuming there is a way to this via delegates.xml, can I do it on a per-user basis or do I have to change the system-wide one?

Re: Convert multipage pdf to animated gif (macports)

Posted: 2013-03-20T13:57:17-07:00
by snibgo
snibgo wrote:... but changing the working version to match the other will probably do the trick.
Oops. I meant change the non-working version, as I'm sure your guessed.

You might search on this forum for "pnmraw" and "pngalpha". I don't understand the issue, nor do I understand GS, but changing one for the other is sometimes advised.

For delegates.xml and other configuration files, see http://www.imagemagick.org/script/resources.php . Seems to me that user files are read after system-wide files, so override settings.

Re: Convert multipage pdf to animated gif (macports)

Posted: 2013-03-20T15:49:40-07:00
by fmw42
The mac versions being more recent, I would guess it's a matter of implementation. The only major difference I see between the two is -sDEVICE=pnmraw for the linux version and -sDEVICE=pngalpha for the mac. Can I tell convert to pass this option to gsx?


You can edit the delegates.xml file and change the sDEVICE to pnmraw on your Mac.

pngalpha only allows one page if the pdf has transparency. pnmraw will allow multiple pages but not transparency or so I am led to believe.

Re: Convert multipage pdf to animated gif (macports)

Posted: 2013-03-21T04:43:22-07:00
by leingang
Thanks for the important distinction between the pnmraw and pngalpha devices.

I have to correct what I said above. Both my linux and mac machines have these two lines in their delegates.xml file:
  • A "ps:color" decoder with the "-sDEVICE=pnmraw" option sent to ghostscript
  • A "ps:alpha" decoder with the "-sDEVICE=pngalpha" option sent to ghostscript
But on the linux machine, the former is applied (accommodating the multipage PDF), while on the mac, the latter (ignoring all pages but the first).

If I were to edit my mac's delegates.xml file, it seems I should either edit the "ps:alpha" decoder line to use "-sDEVICE=pnmraw" or remove the "ps:alpha" line altogether. I worry about breaking some other behavior if I do that.

Is there another way to instruct IM to use a specific decoder?

Re: Convert multipage pdf to animated gif (macports)

Posted: 2013-03-21T13:49:26-07:00
by fmw42
I would not know any other means.

If your PDFs have transparency, the try editing ps:alpha to use pnmraw and see what happens. You can always change it back.

It surprizes me if both systems have the same SDEVICEs for those two lines and you get different results. What about the versions of Ghostscript? Are they the same. Perhaps GS has been updated and allows it to work properly either way now. Is your Linux GS more current than your Mac GS.

Re: Convert multipage pdf to animated gif (macports)

Posted: 2013-03-26T18:11:24-07:00
by leingang
fmw42 wrote:What about the versions of Ghostscript? Are they the same. Perhaps GS has been updated and allows it to work properly either way now. Is your Linux GS more current than your Mac GS.
Actually, no. The mac version is 9.06 (2012-08-08) and the linux is three years older, 8.70 (2009-07-31).

I think it's going to be easier for me personally to combine pdftk with convert than to reconfigure ImageMagick to do this in one call to convert. But thanks for looking into it for me.