Previously, when writing Articles addicted to GIF animation, [Details of GIF format](http://www.tohoho-web.com/wwwgif. I found a great article called htm # ImageBlock). Each image in the GIF file has a delay time set in 2 bytes, and it seems that it can be set in 0.01 second units.
It is natural that there is image data in the GIF file, but it seems that there is an 8-byte block called ** Graphic Control Extension ** in front of the image data. Graphic Control Extension --Extension Introducer (1B) = 0x21 <= mark 1 --Graphic Control Label (1B) = 0xf9 <= mark 2 --Block Size (1B) = 0x04 <= mark 3
I checked the GIF animation with the delay time set to 1 second with a binary editor.
There is a delay time 0x6400
following the marks 1-30x21f904
properly.
Since it is set in ** 1 second **, it is ** 100 ** when divided by ** 0.01 **, so 100 = 0x0064
, and when stored in little endian, it is definitely 0x6400
.
You can change the display time for each frame ** by changing this value.
ChangeDelay.py
#Open the GIF file as binary data.
FileName = 'test.gif'
with open(FileName,'rb') as f:
Bin = f.read()
#Find the Graphic Control Extention and record the position and delay value in the List.
GCE = b'\x21\xf9\x04'
DelayList = []
point = 0
while point < len(Bin):
start = Bin.find(GCE,point)
if start<0:break
delay = int.from_bytes(Bin[start+4:start+6],byteorder="little")
DelayList.append([start+4,delay])
point = start+1
#Change the first one to 1 second and the latter half to 5 times the length.
DelayList[0][1]=100
for i in range(1,len(DelayList)//2):
DelayList[-i][1]*=5
#Rewrite binary data.
for point,delay in DelayList:
Bin = Bin[:point] + delay.to_bytes(2,byteorder="little") + Bin[point+2:]
#Save binary data as GIF.
with open('temp.gif','wb') as f:
f.write(Bin)
Before All 20 images are set to 0.05 seconds.
After The first one was set to 1 second, and the latter half was rewritten 5 times.
Recommended Posts