-
Notifications
You must be signed in to change notification settings - Fork 0
/
crop_grains.py
247 lines (157 loc) · 5.51 KB
/
crop_grains.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from PIL import Image
import png
import sys
from printy import printy
from progress.spinner import Spinner
grain_pixels = []
total_pixels = []
# ARGUMENTS
from_photo = False
if __name__ == "__main__":
for i, arg in enumerate(sys.argv):
if (arg == "--photo"):
from_photo = True
def crop_grains(input, output, x, y):
print("[", end='')
printy("🔥[oB]MineralView@", end='')
print("]", end='')
printy(" [g]selecting grain:@ [wB]" + input + "@")
global spinner
spinner = Spinner('Selecting from [' + str(x) + ", " + str(y) + "]")
sys.setrecursionlimit(10 ** 6)
image = Image.open(input)
global image_rgb
image_rgb = image.convert("RGB")
global height
height = image.height
global width
width = image.width
global coords
coords = [None] * (width+2)
for i in range(width+2):
coords[i] = [True] * (height+2)
find_boundary_initial(x, y)
# export to png output
p = convert_array(width, height)
#print(p.__str__())
f = open(output, 'wb')
w = png.Writer(width, height, greyscale=False, transparent=(0,0,0))
w.write(f, p)
print('', end="\r")
print("[", end='')
printy("🔥[oB]MineralView@", end='')
print("]", end='')
printy(" [g]outputted grain:@ [wB]" + output + "@")
def is_valid_color(x, y):
# Checks that the pixel doesn't already exist
if (x < width and x >= 0 and y < height and y >= 0):
r,g,b = image_rgb.getpixel((x, y))
if (r < 100):
return True
else:
return False
else:
return False
def find_boundary_initial(x, y):
if (x < width and x >= 0 and y < height and y >= 0):
if (from_photo) :
r, g, b = photo_rgb.getpixel((x, y))
else:
r, g, b = image_rgb.getpixel((x, y))
grain_pixels.__add__([x, y, r, g, b])
if is_valid_color(x+1, y):
find_boundary2(x+1, y, 0, 1)
if is_valid_color(x, y+1):
find_boundary(x, y+1, 1, 1)
if is_valid_color(x - 1, y):
find_boundary(x - 1, y, 2, 1)
if is_valid_color(x, y-1):
find_boundary(x, y-1, 3, 1)
def find_boundary(x, y, d, rec):
if (x < width and x >= 0 and y < height and y >= 0):
if (from_photo):
r, g, b = photo_rgb.getpixel((x, y))
else:
r, g, b = image_rgb.getpixel((x, y))
if (x < coords.__len__()):
if (y < coords[x].__len__()):
if (coords[x][y] != None):
grain_pixels.append([x, y, r, g, b])
coords[x][y] = None
# for debugging
# print(x, y)
# print("~~~~~~~~~")
if d != 1:
if is_valid_color(x - 1, y):
find_boundary(x - 1, y, 0, rec+1)
if d != 2:
if is_valid_color(x, y+1):
find_boundary(x, y+1, 3, rec+1)
if d != 3:
if is_valid_color(x, y-1):
find_boundary(x, y-1, 2, rec+1)
def find_boundary2(x, y, d, rec):
if (x < width and x >= 0 and y < height and y >= 0):
if (from_photo):
r, g, b = photo_rgb.getpixel((x, y))
else:
r, g, b = image_rgb.getpixel((x, y))
if(x < coords.__len__()):
if (y < coords[x].__len__()):
if (coords[x][y] != None):
spinner.next()
grain_pixels.append([x, y, r, g, b])
coords[x][y] = None
# for debugging
# print(x, y)
# print("~~~~~~~~~")
if d != 0:
if is_valid_color(x+1, y):
find_boundary2(x+1, y, 1, rec+1)
if d != 2:
if is_valid_color(x, y+1):
find_boundary2(x, y+1, 3, rec+1)
if d != 3:
if is_valid_color(x, y-1):
find_boundary2(x, y-1, 2, rec+1)
def convert_array(width, height):
# create an array of size WIDTH x HEIGHT
result = [None] * height
for i in range(height):
result[i] = [0] * width * 3
#print(result.__str__())
# insert the grain_pixels
for i in range(grain_pixels.__len__()):
pixel = grain_pixels[i]
row = result[pixel[1]]
row[pixel[0]*3] = pixel[2]
row[pixel[0]*3+1] = pixel[3]
row[pixel[0]*3+2] = pixel[4]
return result
if from_photo:
print("[", end='')
printy("🔥[oB]MineralView@", end='')
print("]", end='')
printy(" [w]Enter the grain photo path: @", end='')
global photo_val
photo_val = input("")
print("[", end='')
printy("🔥[oB]MineralView@", end='')
print("]", end='')
printy(" [w]Enter the colorized image path: @", end='')
input_val = input("")
print("[", end='')
printy("🔥[oB]MineralView@", end='')
print("]", end='')
printy(" [w]Enter the output path: @", end='')
output_val = input("")
print("[", end='')
printy("🔥[oB]MineralView@", end='')
print("]", end='')
printy(" [w]Enter the center coords separated by a comma: @", end='')
center_coords = input("")
if from_photo:
photo_image = Image.open(photo_val)
global photo_rgb
photo_rgb = photo_image.convert("RGB")
crop_grains(input_val, output_val, int(center_coords.split(",")[0]), int(center_coords.split(",")[1]))