-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathImg.py
443 lines (386 loc) · 16.1 KB
/
PathImg.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# Author: Nick Sidney Lemberger
# rep ---> github.com/Holysocks/LazerImg
import math
import os
#import sys
import time
from sys import stdout
from PIL import Image,ImageFilter
import plotlib
import argparse
import numpy as np
timeStart = float(time.time())
show_przss1 = True
show_przss2 = True
show_przss3 = True
show_connects = False
square = 10
deltamax = 3
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", required=True, help="Path to file to convert")
ap.add_argument("-n", "--name", required=True, help="Set name of output file without postfix ")
ap.add_argument("-d", "--dpi", required=True, help="Set DPI of input. Use this to scale the output")
ap.add_argument("-f", "--feedrate", required=True, help="Sets max feedrate.")
ap.add_argument("--hpgl", help="Set output protocol to HPGL instead of gcode", action="store_true")
ap.add_argument("-t", "--threshold", help="Set threshold for black/white conversion 0<t<255. Default: 128 ")
ap.add_argument("-offX", "--offsetX", help="Set offset. x Default: 0,0 ")
ap.add_argument("-offY", "--offsetY", help="Set offset. y Default: 0,0 ")
ap.add_argument("--show", help="Usage: --show ""<allThingsToShow>"" things to show: bw, edges, paths")
ap.add_argument("--debugg", help="Shows various debugg-messages", action="store_true")
ap.add_argument("-s", "--scale", help="Scales windows. Default 1")
ap.add_argument("-ch", "--closeHoles", help="close small holes")
ap.add_argument("--delta", help="set max error for reduction of pixel stairs")
args = ap.parse_args()
hpgl = False
file_path = os.path.join(os.path.dirname(__file__), args.path)
filename = args.name
threshold = 128
if args.hpgl: hpgl = True
else: hpgl = False
gcode = not hpgl
if args.threshold: threshold = int(args.threshold)
offset_x, offset_y = 0, 0
if args.offsetX: offset_x = float(args.offsetX)
if args.offsetY: offset_y = float(args.offsetY)
feedrate = args.feedrate
renderScale = 1 # TODO implement auto scale
if args.scale: renderScale = float(args.scale)
inch = 25.4 # in mm
unit = 40 # 1mm = 40 hpgl base units
pxScale = inch / float(args.dpi)
if args.closeHoles:
closeHolesStrength = int(args.closeHoles)
if closeHolesStrength % 2 == 0:
print("--closeHoles value must be an odd integer!")
print("Using next smaller integer {0} instead".format(closeHolesStrength-1))
closeHolesStrength = closeHolesStrength -1
else: closeHolesStrength = 0
if args.show: show_przss1 = show_przss2 = show_przss3 = 1
else: show_przss1 = show_przss2 = show_przss3 = 0
if args.delta: deltamax = float(args.delta)
im = Image.open(file_path).rotate(180).transpose(Image.FLIP_LEFT_RIGHT)
im = im.convert('RGB')
pixel = im.load()
output_path = os.path.join(os.path.dirname(__file__), "output/")
size_x = int(im.size[0])
size_y = int(im.size[1])
totalPx = size_x * size_y
print "Pixel: %spx X %spx " %(size_x, size_y)
print "Size: %.2fmm X %.2fmm "%(size_x * pxScale, size_y * pxScale)
if hpgl: target = open(output_path + filename + '.hpgl', 'w')
if gcode: target = open(output_path + filename + '.nc', 'w')
target.seek(0)
plotter = plotlib.plot(int(size_x / renderScale), int(size_y / renderScale)) # just for visualization and debugging
plotter.setBackground(0, 0, 0)
precalc = math.sqrt(3) * 255
if (threshold >= 255):
threshold = 255
elif (threshold <= 0):
threshold = 0
# Header of Gcode
if gcode:
target.write('/#############################################################/ \n')
target.write('/########## Gcode generated with PathImg.py V0.6 #############/ \n')
target.write('/##### written by Nick Sidney Lemberger aka Holysocks ########/ \n')
target.write('/#############################################################/ \n\n')
target.write('/%s/ \n' % (filename))
target.write('\n\n\n')
target.write('G95 M%s \n' % (feedrate))
# target.write('G28\n')
# target.write('G00 X0 Y0 Z0 \n')
# Header of Gcode
if hpgl:
target.write('IN;SP1;PU0,0')
def thresValue(value): # returnes 0 or 255 for given value, depending on threshold
if value >= threshold:
return 255
else:
return 0
def plotL(x, y, r, g, b, s): # simple visualisazion
plotter.setColor(r, g, b)
plotter.plotdot(x / renderScale, (size_y - y) / renderScale)
if (s):
plotter.show()
print "converting image to black&white..."
for j in xrange(size_y): # First process: convert image to B&W
for i in xrange(size_x):
pixelRGB = pixel[i, j]
tmp = int((math.sqrt(math.pow(pixelRGB[0], 2) + math.pow(pixelRGB[1], 2) + math.pow(pixelRGB[2],
2))) / precalc * 255) # length of color-vector
pixel[i, j] = (thresValue(tmp), thresValue(tmp), thresValue(tmp)) # replacing old pixels
if show_przss1:
plotL(i, j, thresValue(tmp), thresValue(tmp), thresValue(tmp), False)
print "converting: %.2f%s\r" % (100 * (float(j + 1)) / size_y, "% "),
stdout.flush()
plotter.show()
print "converting complete!"
if closeHolesStrength > 0:
im = im.filter(ImageFilter.SMOOTH)
im = im.filter(ImageFilter.SHARPEN)
im = im.filter(ImageFilter.DETAIL)
im = im.filter(ImageFilter.MedianFilter(closeHolesStrength))
im = im.filter(ImageFilter.SMOOTH)
im = im.filter(ImageFilter.DETAIL)
im = im.filter(ImageFilter.MedianFilter(closeHolesStrength))
for j in xrange(size_y): # First process: convert image to B&W
for i in xrange(size_x):
pixel[i, j] = im.getpixel((i, j))
if show_przss1:
plotL(i, j, pixel[i, j][0],pixel[i, j][1],pixel[i, j][2], False)
print "converting: %.2f%s\r" % (100 * (float(j + 1)) / size_y, "% "),
stdout.flush()
plotter.show()
#im.save(output_path + filename + '_BW', format="png")
edge = (255, 0, 0) # edges (black to white or white to black) are red
edgeList = [] # contains position of all edges as tupels (x,y)
joined_pixel = (0, 0, 255) # pixel joined into a path, are blue
white = (255, 255, 255)
plotter.setBackground(0, 0, 0)
# Second process: searching edges
print "searching for edges..."
for j in xrange(size_y): # Searching edges in x-diretion
for i in xrange(size_x):
if i < size_x - 1:
this_pixel = pixel[i, j]
next_pixel = pixel[i + 1, j]
if not this_pixel[1] == next_pixel[1]: # if theres a color change...
if this_pixel == white and not next_pixel == edge:
edgePos = (i + 1, j)
pixel[edgePos] = edge
edgeList.append(edgePos)
if show_przss2:
plotL(i + 1, j, 255, 0, 0, False)
elif next_pixel == white and not this_pixel == edge:
edgePos = (i, j)
pixel[edgePos] = edge
edgeList.append(edgePos)
if show_przss2:
plotL(i, j, 255, 0, 0, False)
print "%s %.1f%s\r" % (len(edgeList), (float(j + 1) / size_y) * 50, "% "),
stdout.flush()
plotter.show()
for j in xrange(size_x): # Searching edges in y-diretion
for i in xrange(size_y):
if i < size_y - 1:
this_pixel = pixel[j, i]
next_pixel = pixel[j, i + 1]
if not this_pixel[1] == next_pixel[1]: # if theres a color change...
if this_pixel == white and not next_pixel == edge:
edgePos = (j, i + 1)
pixel[edgePos] = edge
edgeList.append(edgePos)
if show_przss2:
plotL(j, i + 1, 255, 0, 0, False)
elif next_pixel == white and not this_pixel == edge:
edgePos = (j, i)
pixel[edgePos] = edge
edgeList.append(edgePos)
if show_przss2:
plotL(j, i, 255, 0, 0, False)
print "%s %.1f%s\r" % (len(edgeList), (float(j + 1) / size_x) * 50 + 50, "% "),
stdout.flush()
plotter.show()
print "found edges: %s" % (len(edgeList))
print "creating path..."
stdout.flush()
plotter.setBackground(0, 0, 0)
def testIndex(x, y): # prevents out of bounds exception
if x <= -1 or y <= -1 or x >= size_x or y >= size_y:
return False
else:
return True
def closeCircle(x, y, path):
if len(path) <= 7: return (x, y, path)
a = path[0][0]
b = path[0][1]
if x - 3 <= a <= x + 3 and y - 3 <= b <= y + 3:
path.append((a, b, path[0][2]))
return a, b, path
else:
return x, y, path
# Third process: creates a sub-path
def searchPath(x, y):
subPath = []
while (1): # is there an edge next to current edge? ...
dirct = 0
edgeList.remove((x, y)) # processed edges are removed
pixel[x, y] = joined_pixel
# 1. possible position of next edge
if testIndex(x + 1, y + 1) and pixel[x + 1, y + 1] == edge: # ... yes? move to this edge, and continue to next
if show_przss3:
plotL(x, y, 0, 0, 255, False)
x += 1
y += 1
dirct = 1
# 2. possible position of next edge
elif testIndex(x + 1, y - 1) and pixel[x + 1, y - 1] == edge:
if show_przss3:
plotL(x, y, 0, 0, 255, False)
x += 1
y -= 1
dirct = 2
# 3. possible position of next edge
elif testIndex(x - 1, y + 1) and pixel[x - 1, y + 1] == edge:
if show_przss3:
plotL(x, y, 0, 0, 255, False)
x -= 1
y += 1
dirct = 3
# 4. possible position of next edge
elif testIndex(x - 1, y - 1) and pixel[x - 1, y - 1] == edge:
if show_przss3:
plotL(x, y, 0, 0, 255, False)
x -= 1
y -= 1
dirct = 4
# 5. possible position of next edge
elif testIndex(x + 1, y) and pixel[x + 1, y] == edge:
if show_przss3:
plotL(x, y, 0, 0, 255, False)
x += 1
y = y
dirct = 5
# 6. possible position of next edge
elif testIndex(x - 1, y) and pixel[x - 1, y] == edge:
if show_przss3:
plotL(x, y, 0, 0, 255, False)
x -= 1
y = y
dirct = 6
# 7. possible position of next edge
elif testIndex(x, y + 1) and pixel[x, y + 1] == edge:
if show_przss3:
plotL(x, y, 0, 0, 255, False)
x = x
y += 1
dirct = 7
# 8. possible position of next edge
elif testIndex(x, y - 1) and pixel[x, y - 1] == edge:
if show_przss3:
plotL(x, y, 0, 0, 255, False)
x = x
y -= 1
dirct = 8
if dirct == 0:
return closeCircle(x, y, subPath) # returns end of path and created subPath,
# also checks if path is a circle (ends where it starts)
subPath.append((x, y, dirct))
def seachNextEdge(x, y, givenList): # returns edge with smallest distance to the given one
nearestEdge = givenList[0] # note: version 1. Do not use for Entropy > 10% !!
distance = math.sqrt(math.pow(nearestEdge[0] - x, 2) + math.pow(nearestEdge[0] - y, 2))
for i in edgeList: # iterates through a list that contains all edges.
newDistance = math.sqrt(
math.pow(i[0] - x, 2) + math.pow(i[1] - y, 2)) # calculates distance between given edge and current
if newDistance < distance:
distance = newDistance
nearestEdge = i
return nearestEdge
def searchInSquare(x, y):
tmpList = []
for i in xrange(square):
ii = i - square * 0.5
for j in xrange(square):
jj = j - square * 0.5
if testIndex(x + ii, y + jj) and pixel[x + ii, y + jj] == edge:
tmpList.append(pixel[x + ii, y + jj])
if len(tmpList) == 0:
return seachNextEdge(lastPath[0], lastPath[1], edgeList)
else:
return seachNextEdge(lastPath[0], lastPath[1], tmpList)
def vect(path):
path2 = [path[0]]
i1 = 0
if(path[0][:2] == path[-1][:2]): p2 = np.array(path[-2][:2])
else: p2 = np.array(path[-2][:2])
while(i1 < len(path)-1):
p1 = np.array(path[i1][:2])
k = i1
for k in xrange(i1, len(path)):
p3 = np.array(path[k][:2])
d = np.linalg.norm(np.cross(p2 - p1, p1 - p3)) / np.linalg.norm(p2 - p1)
if d > deltamax:
break
path2.append(path[k])
i1 = k
return path2
# Finally, create sub-paths and join them
lastPath = (0, 0, 0)
totalLen = len(edgeList)
joinedSubPaths = []
while len(edgeList) > 0: # as long as there are edges left...
# nextPath = seachNextEdge(lastPath[0],lastPath[1],edgeList) #...search for nearest edge, depending on last point of last path
nextPath = searchInSquare(lastPath[0], lastPath[1]) # same, but searches for edges in a smaller area
if show_connects:
plotter.setColor(0, 50, 0)
plotter.plotline(lastPath[0] / renderScale, lastPath[1] / renderScale, nextPath[0] / renderScale,
nextPath[1] / renderScale)
lastPath = searchPath(nextPath[0], nextPath[1]) # search next path and save endpoint for next iteration
if len(lastPath[2]) > 1: # prevents artifacts (one-pixel-paths / path without direction))
joinedSubPaths.append(vect(lastPath[2])) # joins sub-paths and removes redundant pixels
print "remaining edges: %s %.2f%s \r" % (len(edgeList), 100 - 100 * (len(edgeList) + 0.001) / totalLen, '% '),
stdout.flush()
if show_przss3:
plotter.show()
print "Total number of sub-pathes: %s%s" % (len(joinedSubPaths), ' ' * 20)
current_command = ""
numberOfLines = 0
def writeHPGL(nextPoint_x, nextPoint_y, laser):
global current_command
global numberOfLines
if laser > 0:
if current_command == "PD":
target.write(
',%d,%d' % ((nextPoint_x * pxScale + offset_x) * unit, (nextPoint_y * pxScale + offset_y) * unit))
else:
target.write(
';PD%d,%d' % ((nextPoint_x * pxScale + offset_x) * unit, (nextPoint_y * pxScale + offset_y) * unit))
current_command = "PD"
else:
if current_command == "PU":
target.write(
',d,%d' % ((nextPoint_x * pxScale + offset_x) * unit, (nextPoint_y * pxScale + offset_y) * unit))
else:
target.write(
';PU%d,%d' % ((nextPoint_x * pxScale + offset_x) * unit, (nextPoint_y * pxScale + offset_y) * unit))
current_command = "PU"
numberOfLines += 1
def writeGcode(nextPoint_x, nextPoint_y, laser):
global numberOfLines
if laser > 0:
target.write(
'G01 X%s Y%s S%s\n' % ((nextPoint_x * pxScale + offset_x), (nextPoint_y * pxScale + offset_y), laser))
else:
target.write(
'G00 X%s Y%s S%s\n\n' % ((nextPoint_x * pxScale + offset_x), (nextPoint_y * pxScale + offset_y), laser))
numberOfLines += 1
for i in joinedSubPaths:
lastDirct = 0
lastPoint = [0, 0]
target.flush()
for j in xrange(len(i)):
pathElement = i[j]
if j == 0:
if hpgl: writeHPGL(pathElement[0], pathElement[1], 0)
if gcode: writeGcode(pathElement[0], pathElement[1], 0)
lastDirct = pathElement[2]
lastPoint = (pathElement[0], pathElement[1])
else:
if not (lastDirct == pathElement[2]) or True: #dont use, buggy # simple run-length compression
if hpgl: writeHPGL(pathElement[0], pathElement[1], 1)
if gcode: writeGcode(pathElement[0], pathElement[1], 1000)
elif j == len(i) - 1:
if hpgl: writeHPGL(pathElement[0], pathElement[1], 1)
if gcode: writeGcode(pathElement[0], pathElement[1], 1000)
lastDirct = pathElement[2]
if hpgl:
target.write(';SP0;PU0,0;IN;')
if gcode:
target.write('G01 S0 \n')
target.write('G00 X0 Y0\n')
target.flush()
print "Elapsed time: %.4fs" % (float(time.time()) - timeStart)
print "Number of lines: %d" % (numberOfLines)
print "Done :3"
print "Press Enter to exit."
im.save(output_path + filename, format="png")
raw_input()