[IOS] Change the display time for each frame of GIF animation in Pythonista3.

Introduction

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.

The goal is [Graphic Control Extension]

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

Check with a binary editor

I checked the GIF animation with the delay time set to 1 second with a binary editor.

5A681C6A-B727-43D3-BDCA-E817CD2029F7.png 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.

let's try it

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. 90E284C4-F734-4E82-B99E-6F4636142A32.gif

After The first one was set to 1 second, and the latter half was rewritten 5 times. DEA4F364-7E69-4607-BA68-2711AC552EDF.gif

Recommended Posts

[IOS] Change the display time for each frame of GIF animation in Pythonista3.
Check the processing time and the number of calls for each process in python (cProfile)
Check the operation of Python for .NET in each environment
Summary of stumbling blocks in Django for the first time
Display the timetable of the Morioka bus location system in Pythonista
Get the number of occurrences for each element in the list
[IOS] Disassemble GIF animation with Pythonista3.
Change the list in a for statement
MongoDB for the first time in Python
Change the font size of the legend in df.plot
Match the distribution of each group in Python
Read the output of subprocess.Popen in real time
Output the specified table of Oracle database in Python to Excel for each file
Specify the lighting Model of SCN Material in Pythonista
Change the time zone with Docker in Oracle Database
Register a task in cron for the first time
Display the integrated temperature for each field with Z-GIS
Check for the existence of BigQuery tables in Java
Date the name of the decomposed PDF for each page
Display Matsuya coupon (QR code) with Pythonista for iOS
The story of creating a "spirit and time chat room" exclusively for engineers in the company
Changed the default style (CSS) of pandas data frame output by display in Google Colab
[IOS] GIF animation with Pythonista3. I was addicted to it.
[Understand in the shortest time] Python basics for data analysis
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Display the time in Django according to the user's region (UTC)
Change the log retention period of CloudWatch Logs in Lambda
The image display function of iTerm is convenient for image processing.
Google search for the last line of the file in Python
Get the index of each element of the confusion matrix in Python
Put the process to sleep for a certain period of time (seconds) or more in Python
Settings for running a test each time you save a file in the editor using watchmedo (watchdog)
The story of returning to the front line for the first time in 5 years and refactoring Python Django