There are two PNG files.
One (A file) is a 10x10 size file with a red circle.
The other file (B file) has many white circles and one red circle.
At this time, I am looking for a way to find the x and y positions of the red circles in the B file.
--
The reason I need these is in some program I want to find the x,y position that matches a specific image I have.
This can be done using pyautogui.locateAllOnScreen, but I want to apply it to inactive windows.
So I was looking for a way to take a screenshot of an inactive window and compare it to an image I have.
Please let me know if there is a better way
Thank you
(This was written with Google Translate. There may be errors.)
CodePudding user response:
I am not so sure but I think your problem can be solved using template matching. If you are looking for an exact match, in your case the 10x10 image exactly as it is, then this should work fine.
CodePudding user response:
I would use opencv
. For your application, you could use templates, but they're dodgy. Instead, I'd just go straight to image B, get the contours and return the coordinates of the one containing the color you're seeking.
As an example, consider this image:
import cv2
import os
FILE = os.path.expanduser(r"~\Downloads\demo.png")
RED = (0, 0, 255) # BGR
if __name__ == "__main__":
img = cv2.imread(FILE, -1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
centroids = cv2.connectedComponentsWithStats(gray, 8, cv2.CV_32S)[3]
for center in centroids:
x, y = int(center[0]), int(center[1])
if (tuple(img[y, x, :]) == RED):
print(x, y)
The following outputs:
596 411