我正在尝试使用imagemagick光栅化一些字体,这个命令可以在终端上正常工作:
convert -size 30x40 xc:white -fill white -fill black -font "fonts\Helvetica Regular.ttf" -pointsize 40 -gravity South -draw "text 0,0 'O'" draw_text.gif
使用子进程运行相同的命令以使其自动化不起作用:
try:
cmd= ['convert','-size','30x40','xc:white','-fill','white','-fill','black','-font','fonts\Helvetica Regular.ttf','-pointsize','40','-gravity','South','-draw',"text 0,0 'O'",'draw_text.gif']
#print(cmd)
subprocess.check_output(cmd,shell=True,stderr=subprocess.STDOUT)
except CalledProcessError as e:
print(e)
print(e.output)
Command '['convert', '-size', '30x40', 'xc:white-fill', 'white', '-fill', 'black', '-font', 'fonts\\Helvetica Regular.ttf', '-pointsize', '40', '-gravity', 'South', '-draw', "text 0,0 'O'", 'draw_text.gif']' returned non-zero exit status 4
b'Invalid Parameter - 30x40\r\n'
答案 0 :(得分:8)
我明白了:事实证明 Windows在convert.exe
中有自己的PATH
程序。
以下代码打印b'C:\\Windows\\System32\\convert.exe\r\n'
:
try:
print(subprocess.check_output(["where",'convert'],stderr=subprocess.STDOUT,shell=True))
except CalledProcessError as e:
print(e)
print(e.output)
在终端中运行相同的代码会显示imagemagick的convert
阴影Windows' convert
:
C:\Users\Navin>where convert
C:\Program Files\ImageMagick-6.8.3-Q16\convert.exe
C:\Windows\System32\convert.exe
我在安装ImageMagick后没有重启python,因此它的PATH
仍然指向Windows版本。
使用完整路径:
try:
cmd= ['C:\Program Files\ImageMagick-6.8.3-Q16\convert','-size','30x40','xc:white','-fill','white','-fill','black','-font','fonts\Helvetica Regular.ttf','-pointsize','40','-gravity','South','-draw',"text 0,0 'P'",'draw_text.gif']
print(str.join(' ', cmd))
print('stdout: {}'.format(subprocess.check_output(cmd,shell=True,stderr=subprocess.STDOUT)))
except CalledProcessError as e:
print(e)
print(e.output)