如何创建文件大小较小的动画 gif?

如何创建文件大小较小的动画 gif?

我知道使用 pdflatex 创建动画 gif 的唯一方法是生成每一帧并应用 imagemagicks convert。问题是所有帧中都有很多东西

pdflatex animation.tex -output-format=pdf
pdfcrop animation.pdf
convert -verbose -delay 10 -loop 0 -density 300 animation-crop.pdf animation.gif

在此处输入图片描述

  • 原始尺寸:1708px x 829px;2.5MB
  • 删除[background rectangle/.style={fill=white}, show background rectangle]:1.6 MB(但字符看起来像素化/不好看)
  • 缩小至 1200px:1.8MB
  • 申请优化器:92.92KiB

有什么方法可以用 pdflatex 生成小图像,例如不一直重新绘制轴?

完整示例

\documentclass[varwidth=false, border=2pt]{beamer}

\usepackage{tikz,pgfplots,multido}
\usetikzlibrary{backgrounds}
\pgfplotsset{compat=1.17}
\usepackage{nicefrac}
\pgfplotsset{every axis legend/.append style={
at={(0,0)},
anchor=north east}}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\begin{document}
\multido{\i=0+1}{100}{%
\begin{tikzpicture}[background rectangle/.style={fill=white}, show background rectangle]
    \begin{axis}[
        axis x line=middle,
        axis y line=middle,
        grid = major,
        width=16cm,
        height=8cm,
        grid style={dashed, gray!30},
        xmin=-2.1,   % start the diagram at this x-coordinate
        xmax= 4.1,   % end   the diagram at this x-coordinate
        ymin= 0,     % start the diagram at this y-coordinate
        ymax= 17,    % end   the diagram at this y-coordinate
        xlabel=$x$,
        ylabel=$y$,
        ytick={0,2,3,4,8,9,12,16},
        yticklabels={0,2,3,4,8,9,12,16},
        legend cell align=left,
        legend pos=south east,
        legend style={draw=none},
        tick align=outside,
        enlargelimits=false]
      % plot the function
      \addplot[domain=-2:{-1.5 + (9.5*\i/100)}, red, ultra thick,samples=500] {2^x};
      \addplot[domain=-2:{-1.5 + (7.5*\i/100)}, blue, ultra thick,samples=500] {3^x};
      \addplot[domain=-2:{-1.5 + (6.5*\i/100)}, green, ultra thick,samples=500] {4^x};
    \end{axis}
\end{tikzpicture}
}

\end{document}

答案1

仅用于比较目的!

我通过以下方式重现了你的代码渐近线我得到:

原始文件:~855KB

Gif 文件:~1MB

在此处输入图片描述

import animate;
settings.tex="pdflatex"; 

animation Ani;

real width=16cm;
real height=8cm;

size(width,height,false);
import graph;

real f(real x){return 2^x;}
real g(real x){return 3^x;}
real h(real x){return 4^x;}
int samples=500;

real xmin=-2.1;
real xmax=4.1;
real ymin=0;
real ymax=17;

for(int a=-5; a<=60;++a)
{
save();
picture pic;
path F=graph(f,-2,-1.5+(9.5*a/100),samples),
     G=graph(g,-2,-1.5+(7.5*a/100),samples),
     H=graph(h,-2,-1.5+(6.5*a/100),samples);

draw(pic,F,red+2.5bp);
draw(pic,G,blue+2.5bp);
draw(pic,H,green+2.5bp);
clip(pic,box((xmin,ymin),(xmax,ymax)));

real marginx=.4, marginy=0.05;
for (real i: new real[]{-2,-1.5,-1,-0.5,0.5,1,1.5,2,2.5,3,3.5,4}){
draw((i,ymin)--(i,ymax),linetype(new real[] {10,10})+gray+opacity(30/100)+0.2bp);
draw(scale(0.8)*Label("$"+(string) i+"$",Relative(0)),(i,0)-(0,marginx)--(i,0));
}
for (int j: new int[]{2,3,4,8,9,12,16}){
draw((xmin,j)--(xmax,j),linetype(new real[] {10,10})+gray+opacity(30/100)+0.2bp);
draw(scale(0.8)*Label("$"+(string) j+"$",Relative(0)),(0,j)-(marginy,0)--(0,j));
}

draw(Label("$x$",EndPoint,LeftSide),(xmin,0)--(xmax,0),Arrow(TeXHead));
draw(Label("$y$",Relative(0.99)),(0,ymin)--(0,ymax),Arrow(TeXHead));
add(pic);
Ani.add();
restore();
}
erase();
Ani.movie(BBox(Fill(white)));

我们为什么需要picture pic

-> 因为我们通常希望首先显示图形。

在此处输入图片描述

答案2

使用 LaTeX 生成高质量动画 GIF 后,您可以使用第三方程序(非 LaTeX)来压缩 GIF(“压缩“表示减小文件大小)。

以下网站还可以,只是输出是高度压缩的.mp4,而不是实际的.gif

www.veed.io/tools/video-compressor/gif-compressor

相关内容