running imagemagick convert (console application) from python

你说的曾经没有我的故事 提交于 2019-12-03 22:08:31

问题


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'

回答1:


I figured it out: It turns out that windows has its own convert.exe program in PATH.

The following code prints 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)

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                                                 

.

I did not restart python after installing ImageMagick so its PATH still pointed to the Windows version.

Using the full path works:

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)


来源:https://stackoverflow.com/questions/15016974/running-imagemagick-convert-console-application-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!