Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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].

like image 752
Alpagut Avatar asked Feb 17 '11 11:02

Alpagut


4 Answers

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.

like image 195
minhee Avatar answered Nov 13 '22 21:11

minhee


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

like image 34
Creshal Avatar answered Nov 13 '22 22:11

Creshal


I would suggest u use subprocess it is safer

import subprocess
params = ['convert', 'src_file', 'result_file']
subprocess.check_call(params)
like image 6
Rakesh Avatar answered Nov 13 '22 21:11

Rakesh


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.

like image 3
crodjer Avatar answered Nov 13 '22 22:11

crodjer