简体   繁体   English

使用PyAutoGUI在屏幕上定位图像,无论色调/亮度如何

[英]Using PyAutoGUI to locate an image on screen regardless of the color tone / brightness

I'm looking for a simple way in Python (PyAutoGUI) to locate all the images of a certain type on the screen but here's the catch, each image has a different gradient / color tone and I don't want to take the screen shot of each and every image to locate them on screen. 我正在寻找一种使用Python(PyAutoGUI)的简单方法来在屏幕上定位某种类型的所有图像,但这是要注意的问题,每个图像都有不同的渐变/色调,我不想拍摄屏幕快照每个图像的位置以在屏幕上定位它们。

Here's the region of the screen containing the images I am trying to get the coordinates of: 这是屏幕区域,其中包含我要获取坐标的图像:

As you can see every square has a unique color (the contrast). 如您所见,每个方块都有独特的颜色(对比度)。

So I want to get the coordinate of every square making PyAutoGUI scan just one image. 因此,我想获取使PyAutoGUI仅扫描一张图像的每个正方形的坐标。 Is there any way I could make it ignore the difference in contrast of the images? 有什么办法可以使它忽略图像对比度的差异? Like making it black and white mode or something. 就像将其设为黑白模式一样。

How the code works: 代码的工作方式:

import pyautogui
coordinates = pyautogui.locateAllOnScreen("image.png") # Returns list of coordinates of all images matching image.png

I would try the following: 我会尝试以下方法:

pyautogui.locateOnScreen("image.png", grayscale=True)

Which will ignore color values and simply compaire the contrast of values. 这将忽略颜色值,而只是补偿值的对比度。 This has the added benefit of comparing about 30% quicker but can lead to false positives. 这具有比较快约30%的优点,但可能导致误报。

I know this is 2 years old but for any future humans that find this like me try the confidence argument. 我知道这已经2岁了,但是对于像我这样发现的任何未来人类,请尝试置信度。

import pyautogui
button7location = pyautogui.locateOnScreen('calc7key.png', confidence=0.9)
button7location
Box(left=1416, top=562, width=50, height=41)

Source: https://pyautogui.readthedocs.io/en/latest/screenshot.html#the-locate-functions 来源: https : //pyautogui.readthedocs.io/zh-CN/latest/screenshot.html#the-locate-functions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM