Preface
GIF pictures have now been integrated into our daily online life, WeChat groups, QQ groups, circle of friends...if you don’t agree, you will fight pictures. Are you afraid? Don't worry, as long as you learn Python's GIF reverse playback skills, you are the "King of Doutu".
Let's start directly with the content of this article!
PIL(Python Image Library)
It is a third-party image processing library of python, with very powerful functions, and is almost recognized as the official image processing library of Python.
1 : Import the module of the PIL library
from PIL import Image, ImageSequence #Import the Image and ImageSequence modules of the PIL library
im = Image.open(r'./1.gif') #Note that the gif animation is placed in the same directory of the program
2 : Decompose the GIF image
In order to understand the principle of this program more intuitively, we can save all the pictures of the program's decomposition gif animation.
sequence =[] #Used to store the sequence of GIF decomposed into frames
i=0for f in ImageSequence.Iterator(im): #Cycle through frame by frame in GIF
sequence.append(f.copy())
i+=1
f.save('out_'+str(i)+'.png') #Save each separated frame
3 : Reverse the sequence obtained by the reverse() function
sequence.reverse()
4 : Restore the obtained frame sequence back to GIF:
sequence[0].save(r'./out_zr640.gif',save_all = True, append_images=sequence[1:]) #The reversed gif is saved in the current directory
Show results:
The above is the detailed content of Python to realize GIF reverse playback. For more information about Python GIF reverse playback, please pay attention to other related articles on ZaLou.Cn!
Recommended Posts