简体   繁体   中英

ImageMagick code in Python script

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.

posterize

sudo convert imgIn.jpg -posterize 2 imgOut.jpg

convert to blob

sudo convert imgIn.jpg -matte \\( +clone -fuzz 57% -opaque black -transparent blue \\) -compose DstOut -composite imgOut.jpg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM