PyAutoGUI Image Recognition - Slight issue with pi

2019-09-15 18:54发布

I am trying to stop a possible block in my bot program by using PyAutoGUI's Image Recognition function (pyautogui.locateCenterOnScreen) to recognize a simple 'OK' button, in order to get rid of the possible block error message. However, the function only sometimes works as I have found out by screenshotting different error messages and comparing the pixel color side by side. The result I have found out is that the pixel color on two different 'OK' buttons are usually off by 1, explaining the image recognition function returning None most of the time. I would like to be able to recognize the image even if the color is off by 1 or 2 pixels. I understand there is the tolerance keyword argument, but the position of the button changes on the screen, making it difficult to use the getpixel function, and you can't use tolerance with locateCenterOnScreen(to my understanding). Would there be any suggestions as to how i can get across this problem? Thanks in advance.

1条回答
混吃等死
2楼-- · 2019-09-15 19:38

Try this below ( see here: http://pyautogui.readthedocs.io/en/latest/screenshot.html )

enter image description here

From the comment I know that you have tried that and this didn't solved the issue in your case.

Another idea which could help is when you crop the image of the button down to an absolute minimum required to find it on screen. Usually the colors near the center are more "stable" than shadows.

From the comment I know, that this hasn't solved your issue either.

Do you allow TRANSPARENCY for Windows in your OS (transparency is when you can "see" through what the current application displays what is in background of the window of this application - is a feature which is a default value in many versions of newest operating systems)?

From the comment I know you do.

This can lead to severe recognition problems. Reducing (to what level?) transparency didn't solved the issue, what means that the only right way to go is:

Switch the transparency feature OFF to get stable colors

That's it - problem solved (how to do that for your specific system is another question not related to the subject of image recognition).

All or one from this above should solve your problem and if not you will be probably forced to do own image recognition using another image processing tools next to pyautogui.

ADDENDUM: The shape and color of an [OK] button can depend on many things. If displayed in an Internet browser it can change its size with resizing the page. If a user dialog is created by different applications the shape and color of an [OK] button can also be different if the application doesn't use the system stadards. And as said above the colors can vary if the system uses the transparency feature.

查看更多
登录 后发表回答