Freddie
Freddie

Reputation: 103

Using PIL (Python Image Library) to detect image on screen

I am trying to understand how I can use PIL in Python 2.7 to search the whole screen for a certain image and click on it. I've been searching around and haven't been able to find a solution. I want to create a small GUI with one button in the middle of it that when clicked will search the entire screen for a predefined image. Once the image is found the program will then click in the centre of it and end. In short the program will detect if an image is present on the users screen and click it.

I did find an interesting bit on Sikuli, but that doesn't help me because it's unable to export to an .exe.

The image that the program will look for will most likely be in the same place each time it searches, but I didn't want to hard-code the location as it has the potential to move and I don't want that being an issue later on.

What I need is the code method I would use to search for the image on screen and send back the cords to a variable.

Image explanation/example: Image

Reference image of rifle: Image2

Upvotes: 10

Views: 54993

Answers (3)

Samuel Santos
Samuel Santos

Reputation: 85

I recommend you give a look on PyAutoGUI, a well documented library to control mouse and keyboard, also can locate imagens on screen, find the position, move the mouse to any location and clicks on location, also can simulate drag and drop, type on input fields, give double clicks and much more.

Upvotes: 1

Even I wanted to do the same but using different module - pyautogui. I finally found the solution for my problem and I am sure this solution will also help you. You have to just go to this webpage and read the locate function topic completely and you'll be able to solve your problem.

Upvotes: 3

Kyle
Kyle

Reputation: 663

PIL is the wrong tool for this job. Instead you should look into openCV (open source computer vision), which has fantastic python bindings. Here is a link to an example (in C but should be easy to redo with the python bindings) that does what you are looking for, but even allows the image to be rotated, scaled, etc.

http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html http://docs.opencv.org/doc/tutorials/features2d/detection_of_planar_objects/detection_of_planar_objects.html

Edit:

I assume you are using windows, as your example image looks like window. In this case you can use:

from PIL import ImageGrab
pil_img = ImageGrab.grab()
opencv_img = numpy.array(pil_img)

then use opencv to process the image to find sub image you are looking for.

If you want to do this cross platform, then you will need to use wxWidgets to do the screengrab: https://stackoverflow.com/a/10089645/455532

Upvotes: 11

Related Questions