使用 ImageMagick 在 python 中创建 gif

Creating a gif in python using ImageMagick

我正在尝试使用 python 通过 GraphicsMagick 模块或使用带有 ImageMagick.exe 的 os.system 命令来创建 gif。我在这里找到的最相关的问题是大约 6 年前的这个问题。

Similar Question 6 years old

URL 用于 imagemagick 下载

file:///C:/Program%20Files/ImageMagick-6.9.1-Q16/www/binary-releases.html#windows

GraphicsMagick Download

下面是我的代码(不工作)。我目前正在尝试 ImageMagick 路线,因为上面的问题表明这更 pythonic。任何帮助,将不胜感激。

from pgmagick import Image, ImageList, Geometry, Color 
import os, sys
import glob

#dataDir = sys.argv[1]
#outDir  = sys.argv[2]

dataDir = 'C:\Users\name\Desktop\a'

os.chdir(dataDir)
fileList = glob.glob(dataDir + '\*.jpg')
fileList.sort()

os.system('convert fileList -delay 20 -loop 0 *jpg animated.gif')

我在代码的最后一行收到无效语法错误。

对于以后在 windows 机器上尝试使用 imagemagick 制作 gif 时看到此问题的任何人,这就是我想出的解决方案。我不知道这是否是内存方面最有效的方法,但它确实有效。

import os, sys

dataDir = 'fullpath of directory with images'
os.chdir(dataDir)


os.system('SETLOCAL EnableDelayedExpansion')
#This is the path of the imagemagick installation convert command.  
#The invalid parameter I was  getting was because the computer was trying to 
#use a different convert command.  This code sets convert as a different
#variable and then calls that new variable as the convert command.  
os.system('SET IMCONV="C:\Program Files\ImageMagick-6.9.1-Q16\Convert"')
os.system('%IMCONV% *.jpg animated.gif')

我为简化解决方案所做的一些更改。我没有像我在问题中那样制作文件列表,而是将所有图像放在一个目录中,然后调用命令将这些图像转换为 gif。这是我遇到的最简单的解决方案。