5

I'm using Imagemagick for image enhancement in my project. As I'm a newbie to Imagemagick, I have started it with using command line argument for this package. For further processing, I need to change the below command into python code.

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

I think it is possible using wand package. But is it possible to convert all the arguments using in the above command. Any help is much appreciated. Thanks

Vijesh Venugopal
  • 1,621
  • 15
  • 26
  • Try here: http://stackoverflow.com/questions/4813238/difference-between-subprocess-popen-and-os-system. os.system is generally considered less of a good idea as compared to popen – joel goldstick Jul 12 '16 at 13:24
  • Actually, I couldn't proceed with those options(os.system and popen) as I have to upload the image to be converted dynamically. Is there any other options available? – Vijesh Venugopal Jul 12 '16 at 13:46

2 Answers2

4

You can use os.system() to run terminal commands from 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)

If you want to use the paths dynamically, you can also use the .format() method:

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)
dot.Py
  • 5,007
  • 5
  • 31
  • 52
2

Another option is Wand, an ImageMagick binding for python to convert the command mentioned above. Please refer the below link for more details:

wand docs

Vijesh Venugopal
  • 1,621
  • 15
  • 26