开发者

Use ImageMagick with python. (on a linux system) [duplicate]

This question already has answers here: Can I access ImageMagick API with Python? 开发者_开发问答 (3 answers) Closed 9 years ago.

I want to define a function that "call" imagemagick to convert an image.

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

How can I call and use imagemagick with Python?

I'm running on a linux system, imagemagick is installed, and I'm not using PIL.module because it doesn't handle PPM[p3].


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:

from wand.image import Image

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

See this tutorial as well.


Either use one of the shell interfaces of Python (os.system, subprocess.Popen) to call the imagemagick binary, or try out PythonMagick.


I would suggest u use subprocess it is safer

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


I have not used image magic but you could use os.system to call a shell command:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜