How to realize gif Motion Picture reversal by Python
Today, I would like to share with you how Python realizes the relevant knowledge of gif motion picture reversal. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Development environment: python environment under pycharm + win10
The theme idea is to capture every frame of the gif motion picture, split the whole picture into a picture stream, then reverse the picture stream, and finally recombine the reverse order image stream into a gif motion picture. The following code is affixed:
The image processing library from PIL import Image under # python, the ImageSequence# system module import os# is used to read gif dynamic images im = Image.open ("read the name of the picture") # iterator iter = ImageSequence.Iterator (im) index = 1for frame in iter for gif image streams: # print ("image% d: mode% s, size% s"% (index, frame.mode, frame.size)) per frame # if the saved path does not exist Then create a folder path = "imgs" if path not in os.listdir (): os.makedirs (path) # Save each picture to the imgs folder frame.save (". / imgs/frame%d.png"% index) index + = split gif into a picture stream imgs = [frame.copy () for frame in ImageSequence.Iterator (im)] # output the original image # imgs [0] .save (". / out.gif") Save_all=True, append_images= imgs [1:]) # reverse image stream imgs.reverse () # Save and output imgs [0] .save (". / reverse_out.gif", save_all=True, append_images= imgs [1:])
Then here is the split image stream:
Finally, there is a comparison of the effect pictures. The following is the original picture:
After that, there is a moving picture in reverse:
These are all the contents of the article "how to achieve gif dynamic picture reversal by Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.