将 ImageMagick 与 python 一起使用。(在Linux系统上)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5028216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use ImageMagick with python. (on a linux system)
提问by Alpagut
I want to define a function that "call" imagemagick to convert an image.
我想定义一个“调用”imagemagick 来转换图像的函数。
def convert(filein,fileout):
#imagemagick>convert filein fileout
How can I call and use imagemagick with Python?
如何在 Python 中调用和使用 imagemagick?
I'm running on a linux system, imagemagick is installed, and I'm not using PIL.module because it doesn't handle PPM[p3].
我在 linux 系统上运行,安装了 imagemagick,我没有使用 PIL.module,因为它不处理 PPM[p3]。
采纳答案by Creshal
Either use one of the shell interfaces of Python (os.system, subprocess.Popen) to call the imagemagick binary, or try out PythonMagick.
要么使用 Python 的 shell 接口之一(os.system、subprocess.Popen)来调用 imagemagick 二进制文件,要么尝试使用PythonMagick。
回答by crodjer
I have not used image magic but you could use os.system to call a shell command:
我没有使用过图像魔法,但你可以使用 os.system 来调用一个 shell 命令:
import os
os.system('imagemagick-converting-command filein fileout')
I suggest you go with PythonMagic as Creshal said. It is provided by ImageMagic and thus must be one of the best port available for python.
我建议你像 Creshal 所说的那样使用 PythonMagic。它由 ImageMagic 提供,因此必须是可用于 Python 的最佳端口之一。
回答by Rakesh
I would suggest u use subprocessit is safer
我建议你使用子进程它更安全
import subprocess
params = ['convert', 'src_file', 'result_file']
subprocess.check_call(params)
回答by minhee
Disclaimer: I am the author of Wand.
免责声明:我是魔杖的作者。
You can easily do that using Wand, a simple binding of ImageMagick for Python. For example, the following code converts a PNG image to a JPEG image:
您可以使用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')
See this tutorialas well.
也请参阅本教程。