Python、Imagemagick和subprocess调用

3
尝试在Python脚本中使用子进程调用运行Imagemagick命令。我在/usr/local/Cellar中安装了Imagemagick,因此我尝试了两个选项,因为它似乎找不到convert命令。

顺便说一下,“convert”命令是Imagemagick的一个命令。 :)以下是与之直接相关的代码部分。

import sys, os, subprocess, string
tempo = 8; #this is just for sample purposes, in the actual code this variable is imported from a csv file

# test with just captioning one image and not whole loop
subprocess.call('convert rgb10.png -pointsize 50 -draw "text 180,180 ' + str(tempo) + '" rgb10-n.png')

我也尝试过 "subprocess.call('/usr/local/Cellar/imagemagick/6.8.9-8/bin/c....",因为我认为它可能找不到命令。

此外,当我在终端命令行中运行此命令(而不是在脚本中),它可以正常工作(例如:convert rgb298.png -pointsize 50 -draw "text 180,180 'test' " rgb298-n.png),所以我知道这一定是我的子进程调用的问题。

这是我运行它时收到的错误:

   subprocess.call('/usr/local/Cellar/imagemagick/6.8.9-8/bin/convert rgb10.png -pointsize 50 -draw "text 180,180 ' + str(tempo[9]) + '" rgb10-n.png')
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 470, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 623, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 1141, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

注意:我还尝试过 subprocess.call('python2.6 convert rgb10.png ...

1个回答

3

我已经搞定了,这很傻!

添加了, shell=True

subprocess.call('convert rgb10.png -pointsize 50 -draw "text 180,180 ' + str(tempo) + '" rgb10-n.png', shell=True)

现在对于遇到此问题的其他人来说,它可以工作了。

但实际上它并没有绘制或考虑变量“tempo”,也就是没有处理+ str(tempo)+ 部分


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接