使用python脚本中的imagemagick将tif文件转换为具有特定顺序的gif

时间:2015-03-02 15:34:37

标签: python imagemagick

更新: os.system给出了错误信息,看起来很奇怪。为什么没有C:在图像路径之前?文件夹中绝对有文件。

convert.exe: unable to open image `\\Users\\admin\\Desktop\\test\\1.tif',': No such file or directory @ error/blob.c/OpenBlob/2643. convert.exe: no decode delegate for this image format `\\Users\\admin\\Desktop\\ test\\1.tif',' @ error/constitute.c/ReadImage/555. convert.exe: unable to open image `\\Users\\admin\\Desktop\\test\\2.tif',': No such file or directory @ error/blob.c/OpenBlob/2643. convert.exe: no decode delegate for this image format `\\Users\\admin\\Desktop\\ test\\2.tif',' @ error/constitute.c/ReadImage/555. convert.exe: unable to open image `\\Users\\admin\\Desktop\\test\\3.tif']': No such file or directory @ error/blob.c/OpenBlob/2643. convert.exe: no decode delegate for this image format `\\Users\\admin\\Desktop\\ test\\3.tif']' @ error/constitute.c/ReadImage/555. convert.exe: no images defined `C:\Users\admin\Desktop\test\animated.gif' @ error/ convert.c/ConvertImageCommand/3147.

我想使用imagemagick将tif文件转换为使用下面的python脚本给定订单的gif。但是,当我将图像列表传递给imagemagick命令行字符串convert -alpha deactivate -verbose -delay 50 -loop 0 -density 300 {} {}animated.gif'.format(images, path)时,imagemagick似乎无法识别图像列表。

import os

path = "C:\\Users\\admin\\Desktop\\test\\"
filenames = ["2.tif", "1.tif", "3.tif"]
images = [path + filename for filename in filenames]
os.system('convert -alpha deactivate -verbose -delay 50 -loop 0 -density 300 {} {}animated.gif'.format(images, path))

通常,我可以使用命令行convert -alpha deactivate -verbose -delay 50 -loop 0 -density 300 *.tif animated.gif'来转换当前文件夹的内容。但是就像这样,我无法将tif文件命令指定为我想要的。它会将文件转换为1.tif2.tif3.tif顺序为最终的gif。

那么有没有办法将python列表传递给imagemagick命令行字符串?

1 个答案:

答案 0 :(得分:0)

我想回答我的问题。

经过一番挖掘,我发现imagemagick命令行可以接受像这样的参数 convert -alpha deactivate -verbose -delay 50 -loop 0 -density 300 2.tif 1.tif 3.tif animated.gif',然后很容易给出如下解决方案:

import os

path = "C:\\Users\\admin\\Desktop\\test\\"
filenames = ["2.tif", "1.tif", "3.tif"]
images = " ".join([path + filename for filename in filenames])
os.system('convert -alpha deactivate -verbose -delay 50 -loop 0 -density 300 {} {}animated.gif'.format(images, path))

使用join函数将列表连接到一个字符串,并将其传递给os.system,一切都会好的。