Skip to content

Instantly share code, notes, and snippets.

@BobSynfig
Last active March 18, 2022 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BobSynfig/74bf12e4d0c56f61b9448e8972bcbada to your computer and use it in GitHub Desktop.
Save BobSynfig/74bf12e4d0c56f61b9448e8972bcbada to your computer and use it in GitHub Desktop.
Set GIF animation loops number
#!/usr/bin/python3
# https://en.wikipedia.org/wiki/GIF#Animated_GIF
# The number of loops (0..65535) is located 3 bytes after the string "NETSCAPE2.0"
import sys
sys.argc = property(lambda self: len(self.argv)).__get__(sys, sys.__class__)
if ( (sys.argc == 1) or
((sys.argc == 2) and (sys.argv[1] == '--help'))):
print("Set GIF animation loops number")
print("Usage: ./set_loops.py [image.gif] [loops]")
print("loops = 1..65535 - 0 = infinite loop")
if (len(sys.argv) == 3):
NAB = b'NETSCAPE2.0'
loops = int(sys.argv[2])
gifFile = sys.argv[1]
nLoops = loops.to_bytes(2, 'little')
f = open(gifFile, 'r+b')
data = f.read(2048) #2kB should be enough
if (data.find(NAB) != -1):
offset = data.find(NAB)+13
f.seek(offset)
f.write(nLoops)
f.close()
print('Done')
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment