How do I pixel color search within a rectangle of pixels?

Hi!

Is it possible to scan a rectangle of pixels for the pixel color provided?
I haven’t found a easy way of doing this. At the moment I’m forced to add serveral pixelchecks to my script.

I’m used to using AutoIt on PC wich have this function: https://www.autoitscript.com/autoit3/docs/functions/PixelSearch.htm

No, there is no such facility in Keyboard Maestro.

You can use a couple of For Each actions to scan an area.

Keyboard Maestro Actions.kmactions (2.9 KB)

2 Likes

Worked like a charm though I get the feeling that the script got a bit slower than defining all pixels individually… but doing it this way it’s a lot faster to set up and adjust :slight_smile:

Thx for your help!

It looks like ImageMagick might have something that could work. I noticed that several people have mentioned it on this forum, so I thought I’d mention it.

Haven’t used it myself.

Hi friends. I just working on same project. And have some question. What if we don't know the precise targeted color value? For example, how to find the line with green characters in this screenshot?


In order to do this i need to find (first?) green pixel in this image. But I only know it's green. Dont know the exact color value.
According to this post, I made this kmm.
List pixel green value along a vertical line v0 --Instance__X Instance__Y Instance__Steps Instance__StepLength -=.kmmacros (12.0 KB)
Pick a point, (x,y) on this image, in my KMM it's (Instance__X,Instance__Y), as starting point.
Extract all green value in pixel RGB value every 3 pixels.
Instance__X =1435
Instance__Y =249
Instance__Steps =70
Instance__StepLength =3
But the output is a list of Green value of RGB, cannot check if it's green.
Hope you can help _Metadata

The Pixel condition has a "more green than" option. Use the brightest the grey numbers get as the comparison (or a bit brighter than that).

Note that "more green than" only looks at the green component, so a bright white will be “more green than” a dull green.

Thanks for reply. this is also my concern, cause white font will have a high green value. So I am thinking of better method.

you can also use the pixel function to get the saturation like this:
PIXEL(Saturation,MOUSEX(),MOUSEY())*100
to avoid getting whites.
see:
https://wiki.keyboardmaestro.com/function/PIXEL?s[]=pixel

1 Like

Combine conditions -- use "All of the following are true" with "More green than grey pixel" and "Darker than white pixel".

1 Like

Introduce Pixel(Saturation,,) to my KM.

check Green value greater than and Saturation greater than v1 --Instance__X Instance__Y Instance__Steps Instance__StepLength.kmmacros (21.2 KB)

Macro-Image

If I run this on:

Screen Shot 2022-12-24 at 12.51.31

Success.

If I run this on real-time computer screen, sometimes failed. Cannot get consistent result.

I also have another idea that calculate Hue value from RGB value to check if a point is green.

But the background algorithm is too complex to apply.

It should work if your values are correct. Try simplifying/messing around with the values. If the part you consider is the same as the picture you shared above you don't have to test for the green, the saturation should be enough. also test it with a steplength of 1 to see if this is causing the inconsistencies.

1 Like