簡體   English   中英

從python運行imagemagick轉換(控制台應用程序)

[英]running imagemagick convert (console application) from python

我試圖使用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'

我想通了:事實證明, Windows在PATH有自己的convert.exe程序

以下代碼打印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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM