假设我有一个4D numpy
数组,其中3D子数组是RGB图像。如何将其转换为gif?我宁愿只依赖于著名的Python图像处理库。
样本数据:
import numpy as np
imgs = np.random.randint(0, 255, (100, 50, 50, 3), dtype=np.uint8)
答案 0 :(得分:0)
使用PIL
很简单:
import numpy as np
from PIL import Image
imgs = np.random.randint(0, 255, (100, 50, 50, 3), dtype=np.uint8)
imgs = [Image.fromarray(img) for img in imgs]
# duration is the number of milliseconds between frames; this is 40 frames per second
imgs[0].save("array.gif", save_all=True, append_images=imgs[1:], duration=50, loop=0)
答案 1 :(得分:0)
您还可以使用ImageMagick和OpenCV从一组图像中制作GIF。
def create_gif(inputPath, outputPath, delay, finalDelay, loop):
# grab all image paths in the input directory
imagePaths = sorted(list(paths.list_images(inputPath)))
# remove the last image path in the list
lastPath = imagePaths[-1]
imagePaths = imagePaths[:-1]
# construct the image magick 'convert' command that will be used
# generate our output GIF, giving a larger delay to the final
# frame (if so desired)
cmd = "convert -delay {} {} -delay {} {} -loop {} {}".format(
delay, " ".join(imagePaths), finalDelay, lastPath, loop,
outputPath)
os.system(cmd)
您可以参考pyimagesearch的本教程,以了解更多信息。