python操作福利gif
def getIMGfromGIF():
from PIL import Image, ImageSequence
#使用PIL 把gif的每一帧图拔下来
im = Image.open(r"C:\NotBackedUp\2872f952653fc1b.gif")# original file
index = 1
for frame in ImageSequence.Iterator(im):
frame.save("C:\\NotBackedUp\\frame%d.png" % index)
index += 1
def setIMG2GIF():
#把一堆图合成gif,切换速度定义duration
import imageio
import os
os.chdir(r'C:\NotBackedUp\frames')
path = r'C:\NotBackedUp\frames'
image_folder = os.fsencode(path)
filenames = []
for file in os.listdir(image_folder):
filename = os.fsdecode(file)
if filename.endswith( ('.jpeg', '.png', '.gif') ):
filenames.append(filename)
filenames.sort() # this iteration technique has no built in order, so sort the frames
images = list(map(lambda filename: imageio.imread(filename), filenames))
imageio.mimsave(os.path.join('my_very_own_gif.gif'), images, duration = 0.5) # modify the frame duration as needed
def gifChangeSpeed():
#修改gif速度,你是否觉得gif速度太快而反应不过来?
import imageio
gif_original = r"C:\NotBackedUp\2872f952653fc1b.gif"
gif_speed = r"C:\NotBackedUp\new.gif"
gif = imageio.mimread(gif_original)
imageio.mimsave(gif_speed, gif, fps=2)
if __name__ == "__main__":
# getIMGfromGIF()
# setIMG2GIF()
gifChangeSpeed()