【问题标题】:Use ImageMagick with python. (on a linux system) [duplicate]将 ImageMagick 与 python 一起使用。 (在linux系统上)[重复]
【发布时间】:2011-06-29 00:45:09
【问题描述】:

我想定义一个“调用”imagemagick 来转换图像的函数。

def convert(filein,fileout):
#imagemagick>convert filein fileout

如何在 Python 中调用和使用 imagemagick?

我在 linux 系统上运行,安装了 imagemagick,但我没有使用 PIL.module,因为它不处理 PPM[p3]。

【问题讨论】:

标签: python linux imagemagick


【解决方案1】:

免责声明:我是 Wand 的作者。

您可以使用Wand 轻松做到这一点,这是一个适用于 Python 的 ImageMagick 的简单绑定。例如,以下代码将 PNG 图像转换为 JPEG 图像:

from wand.image import Image

with Image(filename='in.png') as img:
    img.format = 'jpeg'
    img.save(filename='out.jpg')

也请参阅this tutorial

【讨论】:

  • 能不能调整到300%,怎么加flatten,怎么换背景?
【解决方案2】:

我建议你使用 subprocess 更安全

import subprocess
params = ['convert', 'src_file', 'result_file']
subprocess.check_call(params)

【讨论】:

  • 好的答案,如果它已经在磁盘上,你也不需要将它加载到内存中
【解决方案3】:

我没有使用图像魔法,但你可以使用 os.system 来调用 shell 命令:

import os
os.system('imagemagick-converting-command filein fileout')   

我建议您使用 Creshal 所说的 PythonMagic。它由 ImageMagic 提供,因此必须是可用于 python 的最佳端口之一。

【讨论】:

    【解决方案4】:

    要么使用 Python 的一种 shell 接口(os.system、subprocess.Popen)来调用 imagemagick 二进制文件,要么试试PythonMagick

    【讨论】:

    • 谢谢,os.system 完美运行。
    猜你喜欢
    • 1970-01-01
    • 2018-11-25
    • 2020-05-07
    • 2014-12-13
    • 2020-09-13
    • 1970-01-01
    • 2016-12-23
    • 2016-05-18
    • 1970-01-01
    相关资源
    最近更新 更多