Is it possible to use Sikuli to assert that images are the same in GUI-less mode?

Ilya Yevlampiev picture Ilya Yevlampiev · Nov 5, 2012 · Viewed 7.7k times · Source

I have a testing server which runs headless. One test I want is to check that an image served off a particular URL matches some reference image.

Is there an API in Sikuli which can directly accept an image as a stream and compare it with some other image taken from the local resource file? Unfortunately, there is no complete tutorial on Sikuli's Java API, all I've found is tutorials that assume that there is a display available.

I'll be glad to see any examples or at least links to the needed parts of Sikuli javadocs. Also, suggestions for other approaches are welcome.

Answer

JayTheKay picture JayTheKay · Nov 9, 2012

To use Sikuli you need

  1. A base image on which the other image will be searched.
  2. The image which will be searched within the other image.

If image 1 is your local resource image, you can create a org.sikuli.Finder instance with the path to the image and the Region of this image which will be searched. Example (java level):

finder = new Finder("path/to/image", new Region(0, 0, <imgwidth>, <imgheight>));

If image 1 is your stream, you have to make a BufferedImage out of it somehow (I do not know the best way to do this). Then you can make a org.sikuli.ScreenImage from this BufferedImage with the help of an java.awt.Rectangle and an org.sikuli.Region.

finder = new Finder(new ScreenImage(new Rectangle(0,0,<imgwidth>,<imgheight>), bufferedImage), new Region(0,0,<imgwidth>,<imgheight>))

After you created the finder from image 1, you can search image 2 within this image.

Again, you have two possibilities. If the second image is your local resource image, you can create an org.sikuli.Pattern object with the file location:

pattern = new Pattern("path/to/image.png");

Else, if this is your stream, you have to make a BufferedImage out of the stream somehow. You can then create a pattern from this image:

pattern = new Pattnern(bufferedImage);

As a last step, you can now run the finder to search for the pattern:

finder.find(pattern);

You can check if the finder found anything with:

finder.hasNext();

And you should be able to iterate all findings with:

for (Match m : finder):
    //do something with the match

I hope I could help you although your question is already some weeks old.