即时将 GIF 图像转换为 PNG

即时将 GIF 图像转换为 PNG

我在 Linux 上使用 pdflatex 并尝试将 GIF 转换为 PNG。我可以运行脚本来找出图像属性,甚至可以转换从文件夹提供的文件,但我无法写回该文件夹(或者我会手动转换它们)。我有数百个 gif 文件。

我找到了关于将 GIF 转换为 EPS 的很好的说明。这些说明对我来说不起作用,所以我想尝试 PNG 文件,因为我通常使用 PNG 文件。提前感谢您花时间阅读并发表评论。

之前\begin{document},我已经定义过:

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

在我的文档中,我有

\includegraphics{figures/filename.gif}

当我在文档上运行 pdflatex 时,出现错误:

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

因此,我手动向 includegraphics 命令添加了一个边界框:

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

我使用 imagemagick 的识别命令获得了尺寸。

我可以转换该文件,将其写入新文件,并使用 imagemagick 查看它:

$ convert figures/filename.gif gif:figures/filename.png

我认为我在图形规则中指定的“转换”命令实际上并未运行。我做错了什么?

以下是有关我的构建的一些详细信息:

$ 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
talian, nohyphenation, loaded.
(/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}

答案1

您需要启用 shell 转义。使用 MiKTeX-enable-write18和 TeXlive 时都需要启用-shell-escape

您可以使用epstopdf包裹

\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}

使用epstopdf您可以控制是否每次编译时都进行转换或者仅在目标文件丢失时进行转换。

答案2

\DeclareGraphicsRule需要write18启用流才能与操作系统通信。通常禁用此功能,因为这可能很危险。尝试使用相同的源文件pdflatex --shell-escape test.tex。请参阅 Joseph Wright 的关于启用 write18 的博客文章

在我的计算机上,如果\includegraphics{dir/foo.gif}遇到并DeclareGraphicsRule{.gif}{png}...声明了,LaTeX (1) 运行命令;然后 (2) 查找文件,dir/foo_gif-converted-to.png 因此图形规则必须执行此操作。您可以使用

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

但这会将文件写入文件最初所在的同一目录。您说您正在从您没有写权限的其他目录导入文件。因此,请改为

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

因此文件将被写入文档的目录中。但现在它不再存在了dir/!因此,请确保dir/在 TEXINPUTS 路径中(或\graphicspath),然后将其输入为foo.gif。LaTeX 将 (0)foo.gif在 中查找dir/(1) 转换为./foo_gif-converted-to.png,(2)foo_gif-converted-to.png在当前目录中查找。

您的图形规则和问题标题表明您想要使用输出流;也就是说,不创建转换后的文件。我无法让它工作。副作用是您的文档目录中有很多标题丑陋的转换文件,也许您应该首先使用脚本而不是通过 LaTeX 来完成此操作。

答案3

这不是一个“纯”的 LaTeX 答案,但是......

在运行 pdflatex 之前运行的脚本中执行转换魔法,可以从 makefile 中调用(如果您使用它)

#!/bin/bash 

cd figures

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

现在您已将 dir 图形中的所有 gif:s 转换为 png:s。

然后你只需将它们作为普通 png:s 包含在内。

答案4

您还可以将 gif 转换为 flash 文件 (swf)。这比将其保留为 gif 更好,因为 Adob​​e Reader 默认具有兼容性,这提高了可访问性。有许多免费的转换器,例如格式工厂

延斯·诺克尔给出了如何插入 Flash 影片的示例。

相关内容