如何将convert命令更改为python代码

Vij*_*pal 5 python python-2.7 imagemagick-convert wand

我在我的项目中使用 Imagemagick 进行图像增强。由于我是 Imagemagick 的新手,因此我开始使用此包的命令行参数。为了进一步处理,我需要将以下命令更改为Python代码。

convert sample.jpg -blur 2x1 -sharpen 0x3 -sharpen 0x3 -quality 100 -morphology erode diamond -auto-orient -enhance -contrast -contrast-stretch 0 -gamma .45455 -unsharp 0.25x0.25+8+0.065 -fuzz 2% output.jpg
Run Code Online (Sandbox Code Playgroud)

我认为使用 wand 包是可能的。但是是否可以转换上述命令中使用的所有参数。任何帮助深表感谢。谢谢

dot*_*.Py 4

您可以使用os.system()Python 运行终端命令。

import os

command = 'convert sample.jpg -blur 2x1 -sharpen 0x3 -sharpen 0x3 -quality 100 -morphology erode diamond -auto-orient -enhance -contrast -contrast-stretch 0 -gamma .45455 -unsharp 0.25x0.25+8+0.065 -fuzz 2% output.jpg'

os.system(command)
Run Code Online (Sandbox Code Playgroud)

如果你想动态使用路径,也可以使用以下.format()方法:

infile = 'sample.jpg'
outfile = 'output.jpg'

command = 'convert {} -blur 2x1 -sharpen 0x3 -sharpen 0x3 -quality 100 -morphology erode diamond -auto-orient -enhance -contrast -contrast-stretch 0 -gamma .45455 -unsharp 0.25x0.25+8+0.065 -fuzz 2% {}'.format(infile, outfile)

os.system(command)
Run Code Online (Sandbox Code Playgroud)