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

如何将convert命令更改为python代码,python,python-2.7,imagemagick-convert,wand,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

我在项目中使用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
我认为使用魔杖包装是可能的。但是是否可以转换上述命令中使用的所有参数。非常感谢您的帮助。谢谢

您可以使用它从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)
如果要动态使用路径,还可以使用以下方法:


另一个选项是Wand,它是python的ImageMagick绑定,用于转换上述命令。有关更多详细信息,请参阅以下链接:


谢谢你的回答。在这里,我们如何指定文件路径?在我的例子中,要转换的文件是动态上载的。这很有意义。谢谢这里:。通常认为os.system与popen相比不是一个好主意,我无法继续使用这些选项(os.system和popen),因为我必须上传要动态转换的图像。还有其他选择吗?
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)