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

使用subprocess运行相同的命令以使其自动化不起作用:
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中有自己的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阴影窗口“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)

关于python - 从python运行imagemagick转换(控制台应用程序),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15016974/

10-16 03:54