Search code examples
pythonimagemagickgifanimated-gifimagemagick-convert

Can I join gifs with different speed or delay?


I tried to join two different gif with different delay, but the final output.gif has the same delay, not different... Is there a way to have different delays in one gif?

import subprocess
import os


def mk(i, o, delay=100):
    subprocess.call("convert -delay " +
                    delay + " -loop 5 " + i + " " + o, shell=True)


delay = input("Delay (default 100): ")
# mk("*png", "gif1, delay) # I used this to make the 2 gif
# with a delay of 100 and 30 respectively
# and then I joined them with the code below, but I dunno how to
# give them a separate delay... I want them one after the other
# in order of time
mk("*.gif", "output.gif", delay)
os.system("start output.gif")

enter image description here enter image description here


Solution

  • The delay is a setting so it applies to all images following it, and remains set till you change it.

    So let's make a red, green and blue image:

    convert -size 400x250 xc:red  f1.gif
    convert -size 400x250 xc:lime f2.gif
    convert -size 400x250 xc:blue f3.gif
    

    Now set the delay to 100 and animate the first three, then set it to 300 and animate the same three again but with the new delay this time:

    convert -delay 100 f1.gif f2.gif f3.gif \
            -delay 300 f1.gif f2.gif f3.gif animated.gif
    

    enter image description here

    Now examine the delays associated with the various frames:

    identify -format "%f[%s] %T\n" animated.gif
    animated.gif[0] 100
    animated.gif[1] 100
    animated.gif[2] 100
    animated.gif[3] 300
    animated.gif[4] 300
    animated.gif[5] 300
    

    So I guess what you want is:

    convert -delay 200 1.gif -coalesce \( -delay 30 2.gif -coalesce \) animated.gif
    

    enter image description here