[Production] Python expression package split and merge dynamic GIF images (to get a few lines of code)

"Face pack" is currently indispensable means of communication on social software, meaning it is difficult to express in words, and made a "face pack", the other party will be able to take the hint. Here is a small faction expression package crafted, precisely, it is based on the existing expression package on the secondary processed.

The following code in its simplest form (line 10), the above described "expression package" in the production process. First, the dynamic graph into GIF graphics frames. Below is a GIF format dynamic map found on the network.

Python GIF format using the above-described FIG dynamic resolution graphics frame, only need to enter the following code. Wherein the line 1-2 os import library is introduced from the function-PIL Image library. Line 3 is located in the dynamic opening Image.open FIG called "first.gif" of the D drive path. Line 4 is to create a file named "Graphics split" folder, save for graphics frame after the split. 5-12 is the use of exception handling try-except while loop and find, to save the graphics frame "graphic split" folder. Line 12 is split out how much total print graphics frame. The Python split GIF FIG dynamic code 13 will line, relative to other methods is very simple.

import os
from PIL import Image
im = Image.open('D:\\Python\\gif\\first.gif')
os.mkdir('图形拆分') try: i = 0 while True: im.seek(i) im.save('图形拆分/'+str(i)+'.png') i = i +1 except: pass print('共拆解图像帧数'+str(i)) 

Run the code, dynamic FIG put called "first.gif" pattern 36 is split into the following.

Second, the resolution of the graphics secondary processing. For example, marked with their own favorite "lines", where the "wave it out" on the graphic above four words hit split, you can use Photoshop and other graphics tools. If using Photoshop, then, because the graphics is split after the png format, it is displayed directly open "indexing" state, this time by the "image" -> "mode" -> "the RGB color" layer will become png and by "moving" -> "top alignment / right hand margin" multi-frame alignment pattern superimposed layers, and then resort to the "lines" to and stored individually png, following FIG. Of course, do not use Photoshop, add text to use other software is required. Here the graphic stored in the secondary processing "ShapeMerge" folder for easy third-step operation.

第三,Python将第二步中的图形帧合并成GIF动态图。输入如下代码即可:第1行是导入imageio,os模块,第2行建立一个名为images的空文件,用于保存多帧图形。第3行os.listdir()列表化返回“图形合并”文件夹中所有图形名。第4-5行for-in循环读取列表化的图形名。第6行imageio.mimsave()生成GIF格式动态图,duration=0.1表示每帧图形间隔0.1秒。这个Python合并GIF动态图的代码更加简单,才6行。大家可以加下这个群631441315一起交流 ,群里有大量的PDF书籍、教程免费使用!不管是学习到哪个阶段的小伙伴都可以获取到相应的教程!

import imageio, os
images = []
numberlist = os.listdir('图形合并')
for i in range(len(numberlist)):
    images.append(imageio.imread('图形合并/'+numberlist[i])) imageio.mimsave('newfirst.gif',images,'GIF',duration= 

保存和运行上述代码,便得到了Python制作的表情包“出来浪啦”。

当然现在有很多制作“表情包”的软件,不需要这么复杂。但这里主要是感受Python拆分GIF动态图成多个图形帧,以及合并多个图形帧成GIF动态图的过程。

Guess you like

Origin www.cnblogs.com/qingdeng123/p/11715257.html