【问题标题】:second ImageMagick process fails in Python第二个 ImageMagick 进程在 Python 中失败
【发布时间】:2013-03-06 10:58:46
【问题描述】:

操作系统 Ubuntu 12.04 精确,python 2.7.3,金字塔网络框架,IM 6.4.7-1

我有 Python 代码(在 Pyramid Web 框架应用程序中,但这应该无关紧要),它获取捕获的图像 capture.jpg,然后需要对图像执行两个 ImageMagick 处理。第一个是转换以标记图像(有效),第二个是合成以在图像和标签上应用水印(无效)。起初我认为由于图像尚未准备好,第二次操作静默失败,但添加等待计时器表明事实并非如此。知道如何结合这两种操作吗?它们不能组合成一个 shell 命令。

    now = datetime.datetime.utcnow()
    inpfile = "/home/brian/capture.jpg"
    tfile = "/home/brian/watermark.png"
    label = ("SN7 %s" % now.strftime("%Y%m%d%H%M%S")) 
    outfile = ("/home/brian/%s" % now.strftime("CAM_%Y%m%d%H%M%S") + ".jpg")
    args = []
    args += ["-background", "White"]
args += ["-pointsize","42"]
    args += ["label: "+ label]
    args += ["-gravity", "Center"]
args += ["-append"]
subprocess.check_call(["convert",inpfile] + args + [outfile])
time.sleep(5)
imp = []
imp += ["-dissolve", "25"]
imp += ["-gravity", "South"]
subprocess.check_call(["composite"] + [imp] + tfile + outfile + outfile)
    return [] 

【问题讨论】:

  • 为什么不能一次调用?参数是否根据第一个结果动态更新?另外,我们可以假设您的流程在命令行上运行良好吗?最后,第二个子流程的错误代码是什么?如果没有,那么 composite 会完成它的工作,只是不是你期望的那样。

标签: python pyramid imagemagick-convert


【解决方案1】:

我认为您以错误的方式混合了 listina。 imp 已经是一个列表,您在 check_call 调用中将它放在 [] 中。所以你得到一个列表中的列表。 另外tfile 是一个不能连接到列表的字符串,但这应该作为 TypeError 异常可见。 我发现了第三个问题;您将 outfile 作为字符串连接到 outfile,因此第二个进程将没有有效的输入和输出文件。

【讨论】:

  • 谢谢@krase。是的,该过程在命令行上运行良好。并且它们不能在一次调用中组合在一起。事实证明,解决方案是使用 subprocess.call 而不是 subprocess.check_call ,原因我还不完全理解。工作代码: inpfile = args = [] args += ["-background", "White"] ... args += ["-append"] subprocess.call(["convert",inpfile] + args + [ outfile]) imp = [] imp += ["-dissolve", "25"] imp += ["-gravity", "South"] subprocess.call(["composite"] + imp + [tfile] + [输出文件] + [输出文件])
猜你喜欢
  • 2014-03-04
  • 1970-01-01
  • 2013-03-01
  • 2011-05-28
  • 2014-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多