用Mathematica从GIF文件中导入每帧的DisplayDurations
来源:互联网 发布:虎扑推荐淘宝匡威店铺 编辑:程序博客网 时间:2024/05/20 21:44
http://mathematica.stackexchange.com/questions/5361/when-importing-gif-animation-how-to-find-the-correct-list-of-displaydurations
When importing GIF animation, how to find the correct list of “DisplayDurations”?
When importing GIF animations with variable frame durations, the settings for "DisplayDurations" are not recovered correctly. As an example, I'm using the movie from this earlier question:
Here I'm repeating the code to generate it, putting the gradually increasing frame durations in a variable durations
:
frames = Table[Graphics[ Text[Style["Slow Down", FontFamily -> "Futura", FontColor -> Blue, FontSize -> 48], {0, y}], Background -> Cyan, PlotRange -> {{-1, 1}, {.1, 1.5}}], {y, 1.2, .2, -.1}];durations = Append[Range[Length[frames] - 1]/20, 2];Export["slowDownMovie.gif", frames, "DisplayDurations" -> durations, "AnimationRepetitions" -> Infinity];durations//N
{0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 2.}
Now compare to the durations that we get when re-importing the same animated GIF:
newDurations = Import["slowDownMovie.gif", "DisplayDurations"]
{20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20.}
newDurations == durations
False
As expected from this negative result, ListAnimate
also plays the animation at an incorrect, constant frame delay. It seems clear that this is a bug.
Before I start looking for a platform-specific hack to fix this: is there any way to get the correct display durations from a GIF with the Import
function? I looked into the "Frames"
element of the GIF import, but that also seems to have the wrong display duration in it.
I haven't looked at the raw data, but if one has to do that it may be better to find a non-Mathematica hack instead.
Edit
Wolfram support has confirmed it's a bug [TS 19773].
1 Answer
I don't know if you can persuade Import
to return the correct durations, but luckily the format of .gif files is pretty straight forward so it's not that hard to extract the correct durations manually from the raw data.
In an animated gif the frame durations are stored in a so called Graphic Control Extension or GCE preceding each frame. A GCE starts with the byte sequence 21 F9 04
followed by 4 data bytes and is closed by 00
. Of these data bytes, the second and third byte are the frame duration in hundreds of seconds ordered least significant byte first.
So a crude way to extract the frame durations is to read in the file, find the positions of the byte sequence 21 F9 04
and extract the fourth and fifth byte after each of these positions, e.g.
lst = BinaryReadList["~/test.gif"];seq = FromDigits[#, 16] & /@ StringSplit["21 f9 04"];pos = Position[Partition[lst, 3, 1], seq];durations2 = (Extract[lst, pos + 4] + 256 Extract[lst, pos + 5])/100(* ==> {1/20, 1/10, 3/20, 1/5, 1/4, 3/10, 7/20, 2/5, 9/20, 1/2, 2} *)
- 用Mathematica从GIF文件中导入每帧的DisplayDurations
- 从Mathematica到GIF动画导出练习题
- 从JAR中读取GIF文件
- Mathematica如果把图片导出成动态的gif格式?
- sql脚本从文件中导入数据
- 从数据库中导入导出文件
- SQLite从Excel文件中导入数据
- SQLite从Excel文件中导入数据
- SQLite从Excel文件中导入数据
- SQLite从Excel文件中导入数据
- SQLite从Excel文件中导入数据
- 从Flash中导出透明背景的GIF动画
- mathematica中二维表达式的输入
- 从Excel文件中,导入数据到SQL数据库中,很简单,直接用下面的语句:
- 从SVN中导入的web项目中jsp文件全有红色叉号
- 从指定的Excel文件导入
- [gif] 基于gif.h的gif文件生成
- iphone中处理Gif文件
- class-dump工具的使用
- cocos2dx学习链接1
- [LeetCode] House Robber
- 学习笔记3-成员变量和局部变量,静态变量的区别、多态
- SQL标准数据类型
- 用Mathematica从GIF文件中导入每帧的DisplayDurations
- MongoDB开发学习(2)--文档操作、查询和游标
- ggplot2 3.0 分面、一页多图
- 最大回文子序列数
- 判断sqlite是否包含某个字段
- 开始打破常规
- 马尔科夫随机场
- 浅谈echarts
- sed--文本编辑