用Mathematica从GIF文件中导入每帧的DisplayDurations


http://mathematica.stackexchange.com/questions/5361/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:

slowDown

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].

share edit flag
 
2 
  
2 
  
Well, I would mark that as a bug.  –    Ajasja   May 9 '12 at 9:08
1 
  
@Ajasja I agree, but IMHO it is best if the person posting the question doesn't make that call.  –  Mr.Wizard   May 9 '12 at 9:11
 
  
Try to get the durations of this one. I get a set of infinities. I'd say, a bug.  –    Sjoerd C. de Vries   May 9 '12 at 18:35
 
 

1 Answer

up vote 20 down vote accepted

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} *)
share edit flag
 

Your Answer


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值