Last active
March 18, 2022 17:22
-
-
Save BobSynfig/74bf12e4d0c56f61b9448e8972bcbada to your computer and use it in GitHub Desktop.
Set GIF animation loops number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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