Mathematica如果把图片导出成动态的gif格式?

来源:互联网 发布:杨枝甘露网络笑话 编辑:程序博客网 时间:2024/06/10 01:13

这里的例子一定是针对很久以前的版本的Mathematica的, 现在的版本, Export可以直接导出为动态GIF.


</pre><pre code_snippet_id="376694" snippet_file_name="blog_20140604_2_4627778" name="code" class="plain">自定义函数:
</pre><pre code_snippet_id="376694" snippet_file_name="blog_20140604_4_7724677" name="code" class="plain">ManToGif[man_, name_String, step_Integer] := Export[name <> ".gif",  Import[    Export[name <> Which[$OperatingSystem == "MacOSX", ".mov", $OperatingSystem == "Windows", ".avi"],     man],    "ImageList"][[1 ;; -1 ;; step]]  ]


导出的用法和实例:

man = Manipulate[ContourPlot[    q1/Norm[{x, y} - p[[1]]] + q2/Norm[{x, y} - p[[2]]], {x, -2,      2}, {y, -2, 2}, Contours -> 20, PlotRangePadding -> 0,     Frame -> False, PlotPoints -> 40, ImageSize -> 230,     ColorFunction -> "DarkRainbow"], {{q1, -1}, -3, 3}, {{q2, 2}, -3,     3}, {{p, {{-1, 0}, {1, 0}}}, {-1, -1}, {1, 1}, Locator},    Deployed -> True, FrameMargins -> 0];

ManToGif[man, "charge", 2]


通过改变绘制图片的某个参数,得到动态的效果,然后导出成gif,

这在Mathematica中是不直接支持的. 


但是可以通过Import, Export的方式, 把动态图片的帧导出到视频格式的文件中, 再导入Mathematica中, 然后重导出最终生成gif格式

http://community.wolfram.com/groups/-/m/t/86994;jsessionid=F81DC17EA304180AFB319A45B4E93E9A.wlp1?p_p_auth=83wbyBRj

Showcasing Manipulate[…] via .GIF animations

GROUPS:
Mathematica, Wolfram Community, Dynamic Interactivity, Graphics and Visualization, Import and Export
Vitaliy Kaurov
15Votes
When we create Manipulate or Animate and would like to showcase them on Wolfram Community site, a cool way is to make an animated GIF file of it. It would be also great if this GIF file could contain motion of controls, so people can see what they do. We can apply Export function to Manipulate with .AVI or .MOV or .FLV formats. This will make a movie that can show all motions of controls and content of Manipulate. But from a movie to a GIF there are just a few steps. This function below does these steps. It basically exports Manipulate to a movie, imports it as “ImageList”, and then exports it again as a GIF. Because Mac and Windows have different native movie formats we need to auto-detect them. The result of the function is two files – one is a movie and another is an animated GIF saved in default directory. Here is the legend for arguments:
  • man - variable representing Manipulate
  • name - pure name of the file without any extension
  • step - which every frame to pick: 1 - original no compression, 2 – every 2nd compress twice, etc. 

ManToGif[man_name_Stringstep_Integer:=
 Export[name <".gif",
  Import[
    Export[name <Which[$OperatingSystem == "MacOSX"".mov"$OperatingSystem == "Windows"".avi"],
     man],
    "ImageList"][[;-;step]]
  ]
Let’s see how it works on an example. Here is a Manipulate with 4 controls: 2 sliders and 2 locators. 
man Manipulate[ContourPlot[
    q1/Norm[{xyp[[1]]q2/Norm[{xyp[[2]]]{x-2
     2}{y-22}Contours -> 20PlotRangePadding -> 0
    Frame -> FalsePlotPoints -> 40ImageSize -> 230
    ColorFunction -> "DarkRainbow"]{{q1-1}-33}{{q22}-3
    3}{{p{{-10}{10}}}{-1-1}{11}Locator}
   Deployed -> TrueFrameMargins -> 0];
Here is the result of the function:
ManToGif[man"charge"2]



Any suggestions how we can improve this function?
  • To make it work faster
  • To make smaller .GIFs
  • Any other way… 

P.S. - A few things to note:
  • Control the screen size of GIF by controlling size of Manipulate content.
  • The smaller the screen size, the smaller the byte size.
  • By default Export will generate an animation by running the Manipulate through one Autorun cycle.
  • AutorunSequencing is used when a Manipulate expression is exported using Export to a dynamic format.
  • Use AutorunSequencing to specify how autorun should use the controls provided.
  • When a Manipulate output containing explicit bookmarks is exported to a video animation format using Export, the resulting video will be one cycle through the sequence generated by Animate Bookmarks.


    0 0