Skip to main content
 首页 » 编程设计

Python、Imagemagick 和子进程调用

2024年10月24日13itcoder

尝试在 python 脚本中使用子进程调用来运行 Imagemagick 命令。 我在/usr/local/Cellar 中安装了 imagemagick,所以我尝试了两个选项,因为它看起来好像找不到转换命令。

顺便说一下,'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 ...

请您参考如下方法:

...我已经开始工作了,这太傻了!

已添加,shell=True

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

它现在适用于遇到此问题的任何其他人。

除了它实际上没有绘制或考虑变量“tempo”,也就是不处理 + str(tempo) + bit