Vijesh Venugopal Vijesh Venugopal - 2 months ago 12
Python Question

How to change the convert command to python code

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

Answer

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)
Comments