从python运行imagemagick转换(控制台应用程序)

时间:2023-01-18 20:18:50

I am trying to rasterize some fonts using imagemagick with this command which works fine from a terminal:

我试图使用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

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 个解决方案

#1


8  

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

我明白了:事实证明,Windows在PATH中有自己的convert.exe程序。

The following code prints b'C:\\Windows\\System32\\convert.exe\r\n':

以下代码打印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:

在终端中运行相同的代码显示imagemagick的转换阴影Windows'转换:

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.

安装ImageMagick后我没有重启python,所以它的PATH仍然指向Windows版本。

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)

#1


8  

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

我明白了:事实证明,Windows在PATH中有自己的convert.exe程序。

The following code prints b'C:\\Windows\\System32\\convert.exe\r\n':

以下代码打印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:

在终端中运行相同的代码显示imagemagick的转换阴影Windows'转换:

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.

安装ImageMagick后我没有重启python,所以它的PATH仍然指向Windows版本。

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)