从python运行imagemagick转换(控制台应用程序) [英] running imagemagick convert (console application) from python
问题描述
我正在尝试使用imagemagick光栅化一些字体,这个命令在终端上工作正常:
I am trying to rasterize some fonts using imagemagick with this command which works fine from a terminal:
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
使用子流程运行相同的命令使其自动化不起作用:
Running the same command using subprocess to automate it does not work:
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'
推荐答案
我弄清楚了:事实证明 Windows在路径
中有自己的 convert.exe
程序。
I figured it out: It turns out that windows has its own convert.exe
program in PATH
.
以下代码打印 b'C:\\Windows \\System32 \\convert.exe \\\\ n'
:
try:
print(subprocess.check_output(["where",'convert'],stderr=subprocess.STDOUT,shell=True))
except CalledProcessError as e:
print(e)
print(e.output)
在终端中运行相同的代码显示imagemagick的转换
阴影风ows'转换
:
Running the same code in a terminal shows that imagemagick's convert
shadows 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版本。
I did not restart python after installing ImageMagick so its PATH
still pointed to the Windows version.
使用完整路径:
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)
这篇关于从python运行imagemagick转换(控制台应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!