【问题标题】:Convert tif files to gif with specific order with imagemagick in python script在 python 脚本中使用 imagemagick 将 tif 文件转换为具有特定顺序的 gif
【发布时间】:2015-05-03 00:38:38
【问题描述】:

更新: 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 文件转换为 gif 并使用下面的 python 脚本给定命令。但是,似乎当我将图像列表传递给 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 命令行字符串?

【问题讨论】:

  • @unutbu 它给出了错误信息:

标签: python imagemagick


【解决方案1】:

我想回答我的问题。

经过一番挖掘,我发现 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函数将list加入一个字符串,并传递给os.system,一切都会好起来的。

【讨论】:

    猜你喜欢
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 2012-09-02
    • 2011-03-12
    • 1970-01-01
    • 2016-09-09
    相关资源
    最近更新 更多