Finding an image of a certain color

Asked by Csaba Pinter

I use Sikuli to test some widgets, among them a status icon that is green if there are no errors, orange if there is a warning and red if there is an error.
The problem is that Sikuli finds the red icon even if the image of the green one is set as a pattern to find.
I cannot play with positions because there is only one image and it is always at the same position.
It would be nice to either be able to get the color of a certain pixel or the average color of a region, or improve the find function to be able to take colors into account.
I would appreciate any workaround in the meantime too!

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Using newest version of rc3 from
http://sikuli.org/hudson/job/Sikuli-IDE-Windows/lastSuccessfulBuild/artifact/sikuli-ide/build/
???

If you think, it is not possible with the current features of Sikuli, try the Java AWT Robot class, which provides a getColor() method.

Come back, if you need more help.

Revision history for this message
Csaba Pinter (pinter-csaba) said :
#2

Thanks for the quick answer!

Yes, I am using 1.0rc3

How can I access that Java class from a python script? I am not so familiar in this area...

Revision history for this message
Best RaiMan (raimund-hocke) said :
#3

I am using the latest build from the above download and had no problems with red, yellow, green squares.

This is my script:

import java.awt.Robot as JRobot
myRobot = JRobot()

hover("1319649806076.png")
p = getLastMatch().getCenter()
print myRobot.getPixelColor(p.x, p.y), "at", p

hover("1319649881686.png")
p = getLastMatch().getCenter()
print myRobot.getPixelColor(p.x, p.y), "at", p

hover("1319649893970.png")
p = getLastMatch().getCenter()
print myRobot.getPixelColor(p.x, p.y), "at", p

The 3 images are screenshots of some inner part of the respective colored squares on the screen.

Additionally, you can see, how to use AWT Robot.

javadocs: http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html

Revision history for this message
RaiMan (raimund-hocke) said :
#4

... and this is the printout:

java.awt.Color[r=255,g=0,b=0] at (544,332)

java.awt.Color[r=255,g=254,b=0] at (716,334)

java.awt.Color[r=0,g=128,b=0] at (913,341)

Revision history for this message
Csaba Pinter (pinter-csaba) said :
#5

It works, thank you very much!

The fact that I can use the Java classes opens a whole new world to me!

Revision history for this message
Csaba Pinter (pinter-csaba) said :
#6

Thanks RaiMan, that solved my question.