-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindPixels.py
67 lines (49 loc) · 1.93 KB
/
FindPixels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import time
import pyautogui
import cv2
def on_mouse(event, x, y, flags, params):
global top_left_pt, bottom_right_pt, drawing
# Detecting mouse button down event
if event == cv2.EVENT_LBUTTONDOWN:
if top_left_pt is None:
top_left_pt = (x, y)
else:
bottom_right_pt = (x, y)
drawing = False
def get_area_coordinates():
global top_left_pt, bottom_right_pt, drawing
top_left_pt, bottom_right_pt = None, None
drawing = True
windows = pyautogui.getWindowsWithTitle('BlueStacks')
# Take a screenshot using PyAutoGUI
window = windows[0]
# Activate the window to bring it to the front
window.activate()
time.sleep(0.01)
# Get the window's left, top, width, and height attributes
left, top, width, height = window.left, window.top, window.width, window.height
# Take a screenshot of the specific area
screenshot = pyautogui.screenshot(region=(left, top, width, height))
# Save the screenshot
screenshot.save('game_screen.png')
# Read the screenshot with OpenCV
image = cv2.imread('game_screen.png')
# Create a window and set a mouse callback function
cv2.namedWindow('Game Screen')
cv2.setMouseCallback('Game Screen', on_mouse)
while drawing:
# Display the image
cv2.imshow('Game Screen', image)
# Draw the rectangle
if top_left_pt is not None and bottom_right_pt is not None:
cv2.rectangle(image, top_left_pt, bottom_right_pt, (0, 255, 0), 2)
# Breaking out of the loop
if cv2.waitKey(1) & 0xFF == 27: # Escape key
break
cv2.destroyAllWindows()
if top_left_pt and bottom_right_pt:
print("Top Left Point: ", top_left_pt)
print("Bottom Right Point: ", bottom_right_pt)
return top_left_pt, bottom_right_pt
# Run the function
get_area_coordinates()