Reputation: 67
I need to convert an image so as it only shows the color blue as a blob, is there any way I can run this code in a python script? Or how could I go about it?
The following code does the job but I don't know how to link it together, New to code, any advice, direction, links etc would be much appreciated.
sudo convert imgIn.jpg -posterize 2 imgOut.jpg
sudo convert imgIn.jpg -matte \( +clone -fuzz 57% -opaque black -transparent blue \) -compose DstOut -composite imgOut.jpg
Upvotes: 1
Views: 401
Reputation: 180532
You can use subprocess.check_call, passing the args as a list:
from subprocess import check_call
check_call(["sudo","convert","imgIn.jpg", "-posterize","2","imgOut.jpg"])
check_call([ "sudo",'convert', 'imgIn.jpg', '-matte', '(', '+clone', '-fuzz', '57%', '-opaque', 'black', '-transparent', 'blue', ')', '-compose', 'DstOut', '-composite', 'imgOut.jpg'])
You will have to run the script with sudo, if you want to pass the password also you can use Popen writing the password to stdin:
from subprocess import Popen, PIPE
p1 = Popen(["sudo", "-S", "convert", "imgIn.jpg", "-posterize", "2", "imgOut.jp"], stdin=PIPE, stdout=PIPE)
p1.stdin.write("password\n")
out, err = p1.communicate()
Upvotes: 1