# [Numpy-discussion] collecting the bluest pixels

paul taney paultaney@yahoo....
Tue Oct 7 15:27:55 CDT 2008

```Thank you Stefan and Anne for such quick replies.

I am writing a gimp plugin, and if anybody is interested in how do that -- there are only about 10-20 examples that I"ve found -- this plugin is attempting to do raster-to-vector conversion on the bluest pixels.  It outputs SVG and a python tuple.  Current version is at

http://pastebin.com/m2bd8113f

I dunno np.histogram yet, so rather than look that up I tried Anne"s approach.

I get a divide by zero error on

np.divide(image[...,BLU], image[...,GRN], B)

...and I dont understand this well enough to diagnose.  Any ideas?

paul

----

Anne1 = """Well, I can see several approaches. The most direct way to do what you're asking is to use scipy.optimize.bisect to implement the successive approximations. That'll be almost as slow as your current approach, though. Instead, I'd first write a function that measures the "blueness" of each pixel:"""

def blueness(image, linedensity):
A = np.empty(image.shape[:-1], dtype=np.float32)
np.divide(image[...,BLU], image[...,RED], A)  # use three-argument
# divide to reduce the number of float temporaries
B = np.empty(image.shape[:-1], dtype=np.float32)
np.divide(image[...,BLU], image[...,GRN], B)
return np.minimum(A, B)

Anne2 = """
Now, once you have the bluenesses, you can sort them and pull out the
blueness that gives you the percent you want:
"""
bluenesses = np.sort(blueness(image), axis=None)  # axis=None flattens the array
#factor = bluenesses[int((1-wanted_fraction)*len(bluenesses))]
w, h, bpp = image.shape
factor = bluenesses[int((1-(linedensity*6/float(w*h))) * len(bluenesses))]

Anne3 = """
If you want a smarter blueness filter, you could look into using HSV:
you could then specify a distance in hue and a distance in saturation,
based on how relatively important you think they are.
Anne
"""
print "blueness returning factor = %r" % (factor)
return factor

```