# Convert gif image to png on the fly

I am using pdflatex on linux and attempting to convert gif to png. I can run scripts to find out the image attributes and even and convert the files supplied from a folder but I can not write back to that folder (or I'd convert them by hand). I have hundreds of gif files.

I have found great instructions with regard to converting gif to eps. Those weren't working for me, so I thought I'd try png files because I normally use .png files. Thanks in advance for your time for reading and any comments.

Before \begin{document}, I have defined:

\DeclareGraphicsExtensions{.gif, .ps, .eps, .png}
\DeclareGraphicsRule{.gif}{png}{}{convert #1 'png:-'}


Inside my document, I have

\includegraphics{figures/filename.gif}


When I run pdflatex on my document, I get an error:

! LaTeX Error: Cannot determine size of graphic in convert figures/filename.gif 'png:-' (no size specifed).

So, I added a bounding box manually to the includegraphics command:

\includegraphics[0,0][18,18]{figures/filename.gif}


I got the dimensions using imagemagick's identify command.

I am able to convert the file, write it to a new file, and view it using imagemagick:

$convert figures/filename.gif gif:figures/filename.png  I believe that the "convert" command I specified in the graphics rule is not actually running. What am I doing wrong? Here are some details about my build: $ pdflatex test.tex
This is pdfTeX, Version 3.14159-14h-released-20010417 (Web2C 7.3.3.1)
(./test.tex{pdftex.cfg}
LaTeX2e <2001/06/01>
Babel <v3.7h> and hyphenation patterns for american, french, german, ngerman, i
(/usr/share/texmf/tex/latex/base/article.cls
Document Class: article 2001/04/21 v1.4e Standard LaTeX document class
(/usr/share/texmf/tex/latex/base/size10.clo))
(/usr/share/texmf/tex/latex/tools/verbatim.sty)
(/usr/share/texmf/tex/latex/fancyhdr/fancyhdr.sty)
(/usr/share/texmf/tex/latex/graphics/graphicx.sty
(/usr/share/texmf/tex/latex/graphics/keyval.sty)
(/usr/share/texmf/tex/latex/graphics/graphics.sty
(/usr/share/texmf/tex/latex/graphics/trig.sty)
(/usr/share/texmf/tex/latex/config/graphics.cfg)
(/usr/share/texmf/tex/latex/graphics/pdftex.def)))
(/usr/share/texmf/tex/latex/base/makeidx.sty)
Writing index file test.idx
(/usr/share/texmf/tex/latex/misc/pslatex.sty)
(/usr/share/texmf/tex/latex/pdfpages/pdfpages.sty
(/usr/share/texmf/tex/latex/base/ifthen.sty)
(/usr/share/texmf/tex/latex/tools/calc.sty)
(/usr/share/texmf/tex/latex/ms/eso-pic.sty
(/usr/share/texmf/tex/latex/ms/everyshi.sty))))

LaTeX Warning: Unused global option(s):
[8pt,english].

No file test.aux.
(/usr/share/texmf/tex/latex/psnfss/omspzccm.fd)
(/usr/share/texmf/tex/context/base/supp-pdf.tex
(/usr/share/texmf/tex/context/base/supp-mis.tex
loading : Context Support Macros / Missing
)
loading : Context Support Macros / PDF
) (/usr/share/texmf/tex/latex/psnfss/ot1ptmcm.fd)
(/usr/share/texmf/tex/latex/psnfss/omlptmcm.fd)
(/usr/share/texmf/tex/latex/psnfss/omxpsycm.fd)
<use convert figures/remconf_edit.gif 'png:-'> [1{/usr/share/texmf/dvips/confi
g/pdftex.map}] (/usr/share/texmf/tex/latex/psnfss/omsphv.fd) [2] [3] [4]
(./test.aux) )</usr/share/texmf/fonts/type1/bluesky/cm/cmsy10.pfb>{/usr/share/t
exmf/dvips/base/8r.enc}

-
Can't you dump converted images into another folder? Do you absolutely have to grab them from the read-only folder where they are now? –  ajeh Feb 5 '14 at 18:51

You need to enable shell escapes. With MiKTeX you do -enable-write18 and with TeXlive -shell-escape.

You can set up different conversion rules with the epstopdf package

\documentclass{article}
\usepackage{graphicx}
\usepackage{epstopdf}
\epstopdfDeclareGraphicsRule
{.gif}{png}{.png}{convert gif:\SourceFile.\SourceExt png:\OutputFile}
\AppendGraphicsExtensions{.gif}

\begin{document}

\includegraphics{test.gif}

\end{document}


Using epstopdf you can control if the conversion should be done every time you compile or only if the target file is missing.

-
I think the correct name of the option under TeX Live is -shell-escape. –  mpg Nov 6 '10 at 0:16
Yes. I have corrected the answer. –  Martin Heller Nov 6 '10 at 0:21
Thank you for this great example of how to use epstopdf to accomplish this image conversion. –  Amy Nov 8 '10 at 14:54
--shell-escape actually seems to work in both TeX Live and MiKTeX now -- most likely also with just one dash. See this blog post of Joseph Wright's that Matthew Leingang linked to in his answer. –  SamB Dec 31 '10 at 18:28

\DeclareGraphicsRule requires the write18 stream to be enabled in order to talk to the operating system. Ordinarily this is disabled because it can be dangerous. Try the same source file with pdflatex --shell-escape test.tex instead. See Joseph Wright's blog post about enabling write18.

On my machine if \includegraphics{dir/foo.gif} is encountered and DeclareGraphicsRule{.gif}{png}... is declared, LaTeX (1) runs the command; then (2) looks for a file dir/foo_gif-converted-to.png So the graphics rule has to do that. You could use

\DeclareGraphicsRule{.gif}{png}{.png}{convert #1 dirname #1/basename #1 .gif-gif-converted-to.png}


But that will write to the same directory the file is originally in. You said you are importing files from a different directory that you do not have write access to. So instead do

\DeclareGraphicsRule{.gif}{png}{.png}{convert #1 basename #1 .gif-gif-converted-to.png}


So the file will be written into the document's directory. But now it won't be in dir/ anymore! So make sure that dir/ is in the TEXINPUTS path (or \graphicspath) and just input it as foo.gif. LaTeX will (0) find foo.gif in dir/ (1) convert to ./foo_gif-converted-to.png, (2) find foo_gif-converted-to.png in the current directory.

Your graphics rule and question title indicate you want to do with using the output stream; that is, not create the converted files. I couldn't get that to work. The side effect is that you have a lot of ugly-titled converted files in your document's directory, and maybe you should have just done that in the first place with a script rather than through LaTeX.

-
Thank you for your reply. The shell escape produced some additional log output, but I do not see any graphics in the output file. –  Amy Nov 5 '10 at 20:54
Do you have a working example of \DeclareGraphicsRule. I tried converting to pdf, eps, and png. When I use eps, I see some output to the sreen. I see an outline the size of the image and also the text (which appears in the graphics rule) convert figures/figure.gif 'eps:-' . –  Amy Nov 5 '10 at 21:00
@Amy: see edits –  Matthew Leingang Nov 6 '10 at 21:09
Thank you for your insight into this problem. I especially appreciate you trying that the graphics rule. I can definitely solve this issue outside latex, but I really wanted to exhaust all the terrific features within latex first. Thanks a lot for your time. –  Amy Nov 8 '10 at 14:52

This is not a "pure" LaTeX answer but....

Do the convert magic in a script you run before you run pdflatex, that can be called from your makefile (if you use it)

#!/bin/bash

cd figures

for f in *.gif ;
do
convert "$f" "${f/%gif/png}"
done


Now you have converted all the gif:s in dir figures into png:s.

Then you just include them as normal png:s.

-
Any answer that uses purely latex is going to be wrong. Use the right tool for the job--in this case a shell script. –  nimish Nov 7 '10 at 7:19
Thank you very much for taking the time to reply to my post. –  Amy Nov 8 '10 at 14:53

This works with latex:

\documentclass[a4paper]{article}
\usepackage{graphicx}
\DeclareGraphicsRule{.JPG}{eps}{.JPG}{convert #1 eps:-}
\begin{document}
\framebox{\includegraphics[0,0][150,200]{DSC00121-SMALL.JPG}}
\end{document}


in this way:

• latex --shell-escape ... founds JPG file and puts an instruction into dvi file how to process JPG and what to include in page
• dvips -R0 ... actually launches external command and includes output into a page,
• ps file can be converted into pdf using ps2pdf

So, it does work on the fly and does not create "intermediate" files. This is how it should be --- all is well. This is how i managed to ask latex to downsample eps graphics before including them --- for downloadable pdf document:

\documentclass[a4paper]{article}
\usepackage{graphicx}
\DeclareGraphicsRule{.eps}{eps}{.eps}{convert #1 -resample 72x72 eps:-}
\begin{document}
\framebox{\includegraphics{DSC00121.eps}}
\end{document}


Surely, i would like it to work with pdflatex, but for some reason i could not get it working. Sorry, i don't know what's wrong, i am using "plain" pstricks so far, that's why i have a little pdflatex experience.

By the way, this command may be used to insert compressed (eps) images instead of raw ones:

convert -compress zip #1 -resample 72x72 eps2:-
`
-

You could also convert the gif to a flash file (swf). This is better than keeping it as a gif, since Adobe Reader has compatibility by default, which improves accessibility. There are many free converters, such as Format Factory.

Jens Nöckel gives a sample of how to insert a flash movie.

-
To me this doesn't really answer the question, since it doesn't provide a means to "convert GIF to XXX on the fly". Moreover, SWF may be supported by Adobe Reader, but is that the case across all PDF readers? If not, I don't think it makes it "more accessible" in that format. –  Werner Feb 5 '14 at 18:21
You are correct about the compatability of SWF. Although it is compatible with Adobe Reader by default, most PDF readers cannot see the animations of SWF images. However, all PDF readers will pick up the thumbnail. Also, there is a program that runs on Unix command line called gif2swf that can convert the files on the fly. –  goatandsheep Feb 12 '14 at 16:14