【问题标题】:How to change the convert command to python code如何将转换命令更改为 python 代码
【发布时间】:2016-11-14 17:08:22
【问题描述】:

我在我的项目中使用 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

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

【问题讨论】:

  • 在这里试试:stackoverflow.com/questions/4813238/…。与 popen 相比,os.system 通常被认为不是一个好主意
  • 实际上,我无法继续使用这些选项(os.system 和 popen),因为我必须上传要动态转换的图像。还有其他选择吗?

标签: python python-2.7 imagemagick-convert wand


【解决方案1】:

另一个选项是 Wand,一个用于 python 的 ImageMagick 绑定,用于转换上述命令。详情请参考以下链接:

wand docs

【讨论】:

    【解决方案2】:

    您可以使用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)
    

    如果要动态使用路径,也可以使用.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)
    

    【讨论】:

    • 感谢您的回答。这里我们如何指定文件路径?就我而言,要转换的文件是动态上传的。
    • 这是有道理的。谢谢
    猜你喜欢
    • 2023-02-07
    • 1970-01-01
    • 2021-09-23
    • 2019-08-27
    • 2021-10-09
    • 2015-02-18
    • 2013-05-23
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多