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

2023-09-03Python开发问题
3

本文介绍了从 python 运行 imagemagick convert(控制台应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用 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 "fontsHelvetica 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','fontsHelvetica 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
'

推荐答案

我想通了:原来windows在PATH<中有自己的convert.exe程序/code>.

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

以下代码打印 b'C:\Windows\System32\convert.exe ':

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

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:

Running the same code in a terminal shows that imagemagick's convert shadows Windows' convert:

C:UsersNavin>where convert                                                    
C:Program FilesImageMagick-6.8.3-Q16convert.exe                              
C:WindowsSystem32convert.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 FilesImageMagick-6.8.3-Q16convert','-size','30x40','xc:white','-fill','white','-fill','black','-font','fontsHelvetica 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 convert(控制台应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11