Skip to content

Instantly share code, notes, and snippets.

@magicwenli
Last active May 21, 2021 07:10
Show Gist options
  • Save magicwenli/c7a1c891ed0b67353452ddfff1cc0e0b to your computer and use it in GitHub Desktop.
Save magicwenli/c7a1c891ed0b67353452ddfff1cc0e0b to your computer and use it in GitHub Desktop.
使用pillow将cv2读入的图片创建为GIF
from PIL import Image
import cv2
import numpy as np
def make_gif(target_path, images, duration=80):
images[0].save(target_path,
save_all=True, append_images=images[1:], optimize=False, duration=duration, loop=1)
imgs=[]
for i in range(10):
# 随机图片
randomByteArray = bytearray(os.urandom(120000))
flatNumpyArray = numpy.array(randomByteArray)
RandomColor = flatNumpyArray.reshape(200, 200, 3)
imgs.append(RandomColor)
make_gif('src.gif', [Image.fromarray(cv2.cvtColor(x[0], cv2.COLOR_BGR2RGB)) for x in imgs])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment