3

I have an animated gif that I want to loop n times (animation.gif). After the loop, I want to show a static image (static.gif). The animation will be displayed over the web, so the file size needs to be a small as possible.

I tried implementing it with Imagemagick by adding the static image with a zero delay ...

convert -loop 3 animated.gif -delay 0 static.gif newanim.gif

Although the static image is shown in the end, the problem is that after every iteration static.gif is shown for a split second.

Another thing I tried was to add the animation 3 times and the static image at the end. This works perfectly but the file becomes too large especially if the animation is long, and if it is looped many times. For instance, a 6.1mb animation becomes ~18mb.

convert -loop 1 animated.gif animated.gif animated.gif static.gif newanim.gif

I'm using Python in a linux environment to implement this, so if there are programmatic ways of doing this instead of Imagemagick that would work as well.

EDIT: I failed to mention a constraint: it needs to work without any client side programming (Javascript, CSS, etc). It needs to be a purely gif solution. This constraint makes it different from How to stop an animated gif from looping

Community
  • 1
  • 1
Paul
  • 1,128
  • 1
  • 11
  • 19
  • Sorry, the GIF anim format doesn't provide that ability. – PM 2Ring Jul 21 '15 at 15:21
  • If the animation is 6.1MB once, and then 18MB after repeating it three times, then the concatening is very badly implemented. It should re-render, eliminating duplicate frames, then you should stay at ~6MB. – poke Jul 21 '15 at 16:35
  • do you know how gifs manage this? The way I understand it, if there's 10 frames and you concatenate it, it becomes something like: frame 1, frame 2 ... frame 10, frame 1, frame 2, ... frame 10, frame 1, frame 2 ... frame 10. So even if there are a lot of duplicates, they are not consecutive therefore frames can't just be removed. Unless this isn't how it works – Paul Jul 21 '15 at 17:07

2 Answers2

5

No, you can't. The GIF anim format doesn't provide that ability.

There are various ways to do what you want on a Web page, but you'll need to have a separate file for the static image, and you'll need some code (eg JavaScript, or maybe CSS) to display the static image after the desired number of anim loops.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Ok, that's one constraint I failed to mention. I need to make it work without any client side programming (Javascript, CSS, etc). So it needs to be purely gif – Paul Jul 21 '15 at 15:33
  • 4
    If you can't use client-side (or server-side) stuff, and you _have_ to do it as a self-contained GIF, you're only option is what you've already tried - the giant anim containing the multiple copies of the anim, with the static image tacked onto the end. OTOH, there _are_ other animation options, like SVG, but they generally use multiple files. I suppose it _may_ be possible to embed those files into the SVG file, I've never tried... – PM 2Ring Jul 21 '15 at 15:48
  • @Paul: Update. It **is** possible to do frame animation in SVG with a self-contained file, using `data:` URIs to embed the frame data into the SVG file. This doesn't require JavaScript - my tests use SVG's SMIL animation facilities. This sort of animation should be supported on most recent browsers (to some degree...), but for SVG animation to work properly on a Web page the SVG URL needs to be placed in an `` tag rather than an `` tag. Would that be an acceptable solution? – PM 2Ring Jul 22 '15 at 14:18
  • Although that is an interesting solution, it needs to be a gif solution. SVG support is still not 100% and there are cases when some web services block SVGs. – Paul Jul 22 '15 at 17:12
  • @Paul. Oh well, I figured that might be the case - eg, there's now _some_ support for SVG on Android but it's still fairly primitive, and AFAIK limited to static images. Still, I've had a fun day or so playing with frame animation in SVG. :) – PM 2Ring Jul 23 '15 at 12:51
  • 1
    FWIW, you _could_ build a GIF file that uses non-standard extensions to implement custom looping behaviour, but of course that wouldn't be of much use without GIF display software that understands those extensions. For that matter, the standard GIF extensions permit various exotic things, eg putting multiple images per frame to get more than 256 colours per frame, but most image display software doesn't decode such files correctly & simply displays all the images as separate frames. :( – PM 2Ring Jul 23 '15 at 12:59
  • It's a pity there's not so much work on supporting newer gif features. I guess it just became easier to do client side processing instead. – Paul Jul 23 '15 at 16:41
0

I'm pretty sure that your problem with resulting gif size is in tools you are using. I've created those samples, one with animation and another with animation repeated 2 times and got the same size for both. Check it yourself:

single loop two loops

I've used ScreenToGif, it is super buggy and only works on Windows but does its job and can open existing gif or list of images for editing.

If you need solution for Linux take a look at FFmpeg, but I didn't used it myself.

Pang
  • 9,564
  • 146
  • 81
  • 122
Jurijs Kovzels
  • 5,420
  • 3
  • 23
  • 37