简体   繁体   English

如何在python中使用wand阈值图像

[英]How to threshold an image using wand in python

using imagemagick I can threshold an image and specify a percentage. 使用imagemagick我可以阈值图像并指定百分比。

convert one.png -threshold 60% two.png

How can I do the same using wand in python? 如何在python中使用wand做同样的事情?

It seems that they still have not implemented 'evaluate' method. 看来他们还没有实施'评估'方法。 You can use the following workaround: 您可以使用以下解决方法:

import wand.api
import wand.image
import ctypes

MagickEvaluateImage = wand.api.library.MagickEvaluateImage
MagickEvaluateImage.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_double]

def evaluate(self, operation, argument):
  MagickEvaluateImage(
      self.wand,
      wand.image.EVALUATE_OPS.index(operation),
      self.quantum_range * float(argument))

with wand.image.Image(filename='one.png') as img:
  evaluate(img, 'threshold', 0.60)
  img.save(filename='two.png')

I never used wand but this is how to do the thresholding. 我从未使用过魔杖,但这是如何进行阈值处理的。 Basically if you want to threshold an image, you need to turn it into a binary image (black and white). 基本上,如果要对图像进行阈值处理,则需要将其转换为二进制图像(黑白)。

So what you'll do is turn the image to grayscale, check the pixel value if it's above a certain number(lets say 130), if it's above 130 set the value to 255 else set it to 0. 那么你要做的是将图像转换为灰度,检查像素值是否高于某个数字(比方说130),如果高于130,则将值设置为255,否则将其设置为0。

I checked wand documentation and correct me if I'm wrong, it seems there's is no built in way to do the thresholding. 我检查了魔杖文档并纠正我,如果我错了,似乎没有内置的方法来进行阈值处理。

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

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