-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplitImage.py
208 lines (168 loc) · 4.95 KB
/
SplitImage.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
201
202
203
204
205
206
207
208
from pathlib import Path
import os, errno, pyautogui, label_image, MouseMovement
from PIL import Image
#from joblib import Parallel, delayed
# import multiprocessing
# Creates a name Index
nameIndex = 0
# num_cores = multiprocessing.cpu_count ()
# def processImageSplit (x, y, i, width, height, newDir):
# matching = []
#
# for j in range(0, 3):
# image = Image.open(dir)
#
# image = image.crop(
# ((x + (j * width)), (y + (i * height)), ((x + (j * width)) + width), ((y + (i * height)) + height)))
#
# image.save("./" + newDir + "/" + str(nameIndex) + ".png")
#
# check = label_image.checkImage("./" + newDir + "/" + str(nameIndex) + ".png")
#
# if check:
# matching.append(nameIndex)
#
# nameIndex += 1
#
# return matching
#
#
# def processPartialImageSplit (match, x, y, width, height, newDir):
# i = j = 0
#
# matching = []
#
# if not match > 2:
# j = match
# elif not match < 3 and not match > 5:
# i = 1
# j = match - 3
# elif not match < 6 and not match > 8:
# i = 2
# j = match - 6
#
# image = Image.open(dir)
#
# image = image.crop(
# ((x + (j * width)), (y + (i * height)), ((x + (j * width)) + width), ((y + (i * height)) + height)))
#
# image.save("./" + newDir + "/" + str(match) + ".png")
#
# check = label_image.checkImage("./" + newDir + "/" + str(match) + ".png")
#
# if check:
# matching.append(match)
#
# return matching
#
#
# def checkImages (image):
# return label_image.checkImage (image)
def splitImage (newDir, imgToSplit, imgToSplitDir, oldMatchLocations):
"""
:type newDir: The name of the new directory.
:type imgToSplit: The name of the image that needs to be cropped.
:type imgToSplitDir: The name of the directory of the image that needs to be cropped.
:type oldMatchLocations: The previous match locations.
"""
global nameIndex
xS, yS = pyautogui.size ()
if xS == 3840 and yS == 2160:
x = 101
y = 208
width = 189 + 6
height = 189 + 6
elif xS == 1920 and yS == 1080 or xS == 1920 and yS == 1200:
x = 67
y = 138
width = 126 + 4
height = 126 + 4
if not os.path.exists (newDir):
try:
os.mkdir (newDir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
print ("Path created")
else:
print ("Path already exists")
dir = "./" + imgToSplitDir + "/" + imgToSplit + ".png"
matches = []
nameIndex = 0
# pool = multiprocessing.Pool ()
# manager = multiprocessing.Manager ()
# images = []
checks = []
if len (oldMatchLocations) == 0:
for i in range (0, 3):
nameIndex = 3 * i
image = Image.open (dir)
image = image.crop(
(x, (y + (i * height)), x + (3 * width), ((y + (i * height)) + height)))
image.save("./" + newDir + "/" + str (nameIndex) + "Big" + ".png")
check = label_image.checkColumn ("./" + newDir + "/" + str (nameIndex) + "Big" + ".png")
if check:
for j in range (0, 3):
image = Image.open(dir)
MouseMovement.whereToMove(nameIndex)
image = image.crop(
((x + (j * width)), (y + (i * height)), ((x + (j * width)) + width),
((y + (i * height)) + height)))
image.save("./" + newDir + "/" + str(nameIndex) + ".png")
check = label_image.checkImage("./" + newDir + "/" + str(nameIndex) + ".png")
if check:
matches.append(nameIndex)
nameIndex += 1
# i represents columns j represents row
# for i in range (0, 3):
# for j in range (0, 3):
# image = Image.open (dir)
#
# MouseMovement.whereToMove (nameIndex)
#
# image = image.crop (
# ((x + (j * width)), (y + (i * height)), ((x + (j * width)) + width), ((y + (i * height)) + height)))
#
# # images.append (image)
#
# image.save ("./" + newDir + "/" + str (nameIndex) + ".png")
#
# check = label_image.checkImage ("./" + newDir + "/" + str (nameIndex) + ".png")
#
# if check:
# matches.append (nameIndex)
# # MouseMovement.imageClick ()
#
# nameIndex += 1
else:
for match in oldMatchLocations:
i = j = 0
if not match > 2:
j = match
elif not match < 3 and not match > 5:
i = 1
j = match - 3
elif not match < 6 and not match > 8:
i = 2
j = match - 6
image = Image.open (dir)
MouseMovement.whereToMove (match)
image = image.crop (
((x + (j * width)), (y + (i * height)), ((x + (j * width)) + width), ((y + (i * height)) + height)))
image.save ("./" + newDir + "/" + str (match) + ".png")
check = label_image.checkImage ("./" + newDir + "/" + str (match) + ".png")
if check:
matches.append (match)
# MouseMovement.imageClick ()
nameIndex += 1
# checks.append (pool.map (checkImages, images))
# pool.close ()
# pool.terminate ()
# pool.join ()
#
# matches = [i for i, c in checks if c]
# if len (oldMatchLocations) == 0:
# matches.append (Parallel (n_jobs = num_cores)(delayed (processImageSplit)(x, y, i, width, height, newDir) for i in range (0, 3)))
# else:
# matches.append (Parallel (n_jobs = num_cores)(delayed (processPartialImageSplit)(match, x, y, width, height, newDir) for match in oldMatchLocations))
return matches