-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawPD2.py
200 lines (157 loc) · 6.72 KB
/
DrawPD2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from PIL import Image
import pyautogui
import math
import time
import json
pyautogui.PAUSE = 0.001
pyautogui.FAILSAFE = True
isleft = False
SCROLLTIMEOUT = 0.75 # The timeout to allow the scroll animation to complete
def getScreenBounds() -> tuple[tuple[int, int], tuple[int, int]]:
input("Please move your cursor to the top left edge of your drawing Plane: Press enter to contiue ")
topLeft = pyautogui.position()
input("Please move your cursor to the bottom right edge of your drawing Plane: Press enter to continue ")
bottomRight = pyautogui.position()
return((topLeft, bottomRight))
def loadUsersImage():
filename = input(
"What is the name of the image file? (Include the file extension) ")
img = Image.open(filename)
rgb = img.convert("RGB")
return rgb
def getColorsAndPositions(colors, side):
print(f"Please scroll the color selector to the {side} side \n")
iny = input(
"Hover over one of the colors and press enter or 'n' then enter when you are done. ")
if iny == "n":
return colors
x, y = pyautogui.position()
# Didn't return out of the function
im = pyautogui.screenshot()
px = im.getpixel((x, y))
colors.append((px, (x, y)))
return getColorsAndPositions(colors=colors, side=side)
def colorDifference(base: tuple[int, int, int], target: tuple[int, int, int]):
diff = 0
for index, baseColor in enumerate(base):
diff += abs(target[index]-baseColor)
return(diff)
def findClosestColor(colors: list[tuple[int, int, int]], target: tuple[int, int, int]):
bestDifference = 1000
for color in colors:
diff = colorDifference(color, target)
if diff < bestDifference:
bestColor = color
bestDifference = diff
return bestColor
def getCombinedColorsList(left, right):
combined = []
for combo in left:
if combo[0] not in combined:
combined.append(combo[0])
for combo in right:
if combo[0] not in combined:
combined.append(combo[0])
return(combined)
def getRanges(scalefactor, img: Image.Image):
xrange = math.floor(img.width/scalefactor)
yrange = math.floor(img.height/scalefactor)
return((xrange, yrange))
def getButton(side: str):
input(f"Please hover over the {side} button and press enter ")
return pyautogui.position()
def findListColorIsIn(left: list[tuple[tuple[int, int, int], tuple[int, int, int]]], right: list[tuple[tuple[int, int, int], tuple[int, int, int]]], target: tuple[int, int, int]):
# Index 0 is colors
# Index 1 is position
for item in left:
if item[0] == target:
return left, "L"
return right, "R"
def getCoordsOfColor(list: list[tuple[tuple[int, int, int], tuple[int, int, int]]], color: tuple[int, int, int]):
for item in list:
if item[0] == color: # If the color is the color
return item[1]
def selectClosestColor(colors, target: tuple[int, int, int], leftcolors: list[tuple[tuple[int, int, int], tuple[int, int, int]]], rightcolors: list[tuple[tuple[int, int, int], tuple[int, int, int]]], rightPos: tuple[int, int], leftPos: tuple[int, int]):
global isleft
closest = findClosestColor(colors, target)
print(f"{closest} is the closest color to {target}\n Also isleft is {isleft}")
list, targetSide = findListColorIsIn(leftcolors, rightcolors, closest)
if targetSide == "R":
if isleft:
rx, ry = rightPos
# Scroll to the right
pyautogui.moveTo(rx, ry)
pyautogui.click()
isleft = False
time.sleep(SCROLLTIMEOUT)
x, y = getCoordsOfColor(list=list, color=closest)
pyautogui.moveTo(x, y)
pyautogui.click()
else:
if not isleft:
lx, ly = leftPos
# Scroll left
pyautogui.moveTo(lx, ly)
pyautogui.click()
isleft = True
time.sleep(SCROLLTIMEOUT)
x, y = getCoordsOfColor(list=list, color=closest)
pyautogui.moveTo(x, y)
pyautogui.click()
def getColorFromImage(x: int, y: int, image: Image.Image):
return image.getpixel((x, y))
def clickScreenPixel(x, y, xrange, yrange, bounds):
topLeft, bottomRight = bounds
xstep = (bottomRight[0] - topLeft[0]) / xrange
ystep = (bottomRight[1] - topLeft[1]) / yrange
pyautogui.moveTo(topLeft[0] + xstep*x, topLeft[1] + ystep*y)
pyautogui.click()
def askUserToSavePositions(leftButton, rightButton, leftColors, rightColors, combinedColors):
# Ask the user if they want to save the positions if so save it to json
inp = input("Do you want to save the positions? (y/n) ")
if inp == "y":
filename = input("What is the name of the settings file? ")
with open(f"settings/{filename}.json", "w") as f:
json.dump({"leftButton": leftButton, "rightButton": rightButton, "leftColors": leftColors,
"rightColors": rightColors, "combinedColors": combinedColors}, f)
def askUserToLoadPositions():
# Ask the user if they want to load the positions if so load it from json
filename = input("What is the name of the settings file? ")
with open(f"settings/{filename}.json", "r") as f:
data = json.load(f)
return data
def main():
global isleft
inp = input("Do you want to load positions? (y/n) ")
image = loadUsersImage()
if inp == "y":
data = askUserToLoadPositions()
leftButton = data["leftButton"]
rightButton = data["rightButton"]
leftColorsAndPositions = data["leftColors"]
rightColorsAndPositions = data["rightColors"]
combinedColors = data["combinedColors"]
isleft = True
else:
leftColorsAndPositions = getColorsAndPositions([], "left")
rightColorsAndPositions = getColorsAndPositions([], "right")
combinedColors = getCombinedColorsList(
leftColorsAndPositions, rightColorsAndPositions)
leftButton = getButton("left")
rightButton = getButton("right")
isleft = False
bounds = getScreenBounds()
scalefactor = int(
input("What scale factor do you want to use? Lower = More resolution"))
xrange, yrange = getRanges(scalefactor, image)
for x in range(xrange):
for y in range(yrange):
target = getColorFromImage(x * scalefactor, y * scalefactor, image)
selectClosestColor(combinedColors, target,
leftColorsAndPositions, rightColorsAndPositions, rightButton, leftButton)
clickScreenPixel(x, y, xrange, yrange, bounds)
if inp != "y":
askUserToSavePositions(leftButton, rightButton, leftColorsAndPositions,
rightColorsAndPositions, combinedColors)
if __name__ == "__main__":
main()