简体   繁体   English

Python脚本中的ImageMagick代码

[英]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? 我需要转换图像,以便仅将蓝色显示为斑点,有什么办法可以在python脚本中运行此代码? 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 转换为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: 您可以使用subprocess.check_call ,将args作为列表传递:

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: 您将必须使用sudo运行脚本,如果您还想传递密码,则可以使用Popen将密码写入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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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