-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinedraw.py
More file actions
487 lines (396 loc) · 13.4 KB
/
linedraw.py
File metadata and controls
487 lines (396 loc) · 13.4 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# TODO: document it more fully
# This is derived from https://github.com/LingDong-/linedraw
# by Lingdong Huang
# and from Daniele Procida's modifications for BrachioGraph
import os
import json
import time
import math
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
from PIL import Image, ImageOps
# constants
EXPORT_PATH = "images/out.svg"
SVG_FOLDER = "images/"
JSON_FOLDER = "images/"
NO_CV_MODE = False
# Ensure directories exist
os.makedirs(SVG_FOLDER, exist_ok=True)
os.makedirs(JSON_FOLDER, exist_ok=True)
try:
import numpy as np
import cv2
except ImportError as import_error:
print(f"ImportError: {import_error}")
print("Unable to import numpy/openCV. Switching to NO_CV mode.")
NO_CV_MODE = True
# -------------- output functions --------------
def image_to_json(
image_filename,
resolution=1024,
draw_contours=False,
repeat_contours=1,
draw_hatch=False,
repeat_hatch=1,
):
lines = vectorise(
image_filename,
resolution,
draw_contours,
repeat_contours,
draw_hatch,
repeat_hatch,
)
pure_filename = Path(image_filename).stem
filename = filename = Path(JSON_FOLDER) / f"{pure_filename}.json"
lines_to_file(lines, filename)
def make_svg(lines):
print("Generating SVG file...")
width = math.ceil(max([max([p[0] * 0.5 for p in l]) for l in lines]))
height = math.ceil(max([max([p[1] * 0.5 for p in l]) for l in lines]))
out = f'<svg xmlns="http://www.w3.org/2000/svg" height="{height}px" width="{width}px" version="1.1">'
out += "".join(
f'<polyline points="{",".join(f"{p[0] * 0.5},{p[1] * 0.5}" for p in l)}" '
'stroke="black" stroke-width="1" fill="none" />\n'
for l in lines
)
out += "</svg>"
return out
# use turtle graphics to visualise how a set of lines will be drawn
def draw(lines):
from tkinter import Tk, Canvas
from turtle import TurtleScreen, RawTurtle
# Calculate the maximum and minimum coordinates of the lines
max_x = max(max(point[0] for point in line) for line in lines)
min_x = min(min(point[0] for point in line) for line in lines)
max_y = max(max(point[1] for point in line) for line in lines)
min_y = min(min(point[1] for point in line) for line in lines)
# Calculate the range of coordinates
range_x = max_x - min_x
range_y = max_y - min_y
# Set up the Tkinter window and canvas
window = Tk()
canvas = Canvas(window, width=800, height=800)
canvas.pack()
# Set up the Turtle screen and turtle
screen = TurtleScreen(canvas)
turtle = RawTurtle(screen)
turtle.speed(0) # Set the turtle's speed to the fastest
turtle.hideturtle() # Hide the turtle cursor
# Calculate the scaling factors to fit the drawing within the window
scale_x = 750 / range_x
scale_y = 750 / range_y
# Calculate the offset to center the drawing
offset_x = (800 - range_x * scale_x) / 2
offset_y = (800 - range_y * scale_y) / 2
# Iterate over each line and draw it
for line in lines:
turtle.penup()
x, y = line[0]
turtle.goto((x - min_x) * scale_x + offset_x, (max_y - y) * scale_y + offset_y)
turtle.pendown()
for point in line[1:]:
x, y = point
turtle.goto(
(x - min_x) * scale_x + offset_x, (max_y - y) * scale_y + offset_y
)
# Start the Tkinter event loop
window.mainloop()
# -------------- conversion control --------------
def resize_image(image, resolution, draw_option, h, w):
return image.resize(
(int(resolution / draw_option), int(resolution / draw_option * h / w))
)
def vectorise(
image_filename,
resolution=1024,
draw_contours=False,
repeat_contours=1,
draw_hatch=False,
repeat_hatch=1,
):
image = None
possible_paths = [
Path(image_filename),
Path("images") / image_filename,
Path("images") / f"{image_filename}.jpg",
Path("images") / f"{image_filename}.jpeg",
Path("images") / f"{image_filename}.png",
Path("images") / f"{image_filename}.tif",
Path("images") / f"{image_filename}.tiff",
Path("images") / f"{image_filename}.webp",
]
for p in possible_paths:
try:
image = Image.open(p)
break
except Exception:
pass
else:
raise FileNotFoundError(f"Image file not found: {image_filename}")
w, h = image.size
# one-shot convert image to greyscale and max contrast
image = ImageOps.autocontrast(image.convert("L"), 10)
lines = []
with ThreadPoolExecutor() as executor:
if draw_contours:
image_resized = resize_image(image, resolution, draw_contours, h, w)
contours = executor.submit(
get_contours, image_resized, draw_contours
).result()
lines += contours * repeat_contours
if draw_hatch:
image_resized = resize_image(image, resolution, draw_hatch, h, w)
hatches = executor.submit(hatch, image_resized, draw_hatch).result()
lines += hatches * repeat_hatch
pure_filename = Path(image_filename).stem
with open(Path(SVG_FOLDER) / f"{pure_filename}.svg", "w") as f:
f.write(make_svg(lines))
segments = sum(len(line) for line in lines)
print(f"{len(lines)} strokes, {segments} points. Done.")
return lines
# -------------- vectorisation options --------------
def get_contours(image, draw_contours=2):
print("Generating contours...")
image = find_edges(image)
IM1 = np.array(image)
IM2 = np.rot90(IM1, 3)
IM2 = np.flip(IM2, axis=1)
dots1 = get_dots(IM1)
dots2 = get_dots(IM2)
contours1 = connect_dots(dots1)
contours2 = connect_dots(dots2)
for i in range(len(contours2)):
contours2[i] = [(c[1], c[0]) for c in contours2[i]]
contours = contours1 + contours2
for i in range(len(contours)):
for j in range(len(contours)):
if len(contours[i]) > 0 and len(contours[j]) > 0:
if distance_sum(contours[j][0], contours[i][-1]) < 8:
contours[i] = contours[i] + contours[j]
contours[j] = []
for i in range(len(contours)):
contours[i] = [contours[i][j] for j in range(0, len(contours[i]), 8)]
contours = [c for c in contours if len(c) > 1]
for i in range(len(contours)):
contours[i] = [
(v[0] * draw_contours, v[1] * draw_contours) for v in contours[i]
]
return contours
# hatching
def hatch(image, draw_hatch=16):
t0 = time.time()
print("Hatching using hatch()...")
pixels = image.load()
w, h = image.size
horizontal_lines = []
diagonal_lines = []
for x0 in range(w):
for y0 in range(h):
x = x0 * draw_hatch
y = y0 * draw_hatch
# don't hatch above a certain level of brightness
if pixels[x0, y0] > 144:
pass
# above 64, draw horizontal lines
elif pixels[x0, y0] > 64:
horizontal_lines.append(
[(x, y + draw_hatch / 4), (x + draw_hatch, y + draw_hatch / 4)]
)
# above 16, draw diagonal lines also
elif pixels[x0, y0] > 16:
horizontal_lines.append(
[(x, y + draw_hatch / 4), (x + draw_hatch, y + draw_hatch / 4)]
)
diagonal_lines.append([(x + draw_hatch, y), (x, y + draw_hatch)])
# below 16, draw diagonal lines and a second horizontal line
else:
horizontal_lines.append(
[(x, y + draw_hatch / 4), (x + draw_hatch, y + draw_hatch / 4)]
) # horizontal lines
horizontal_lines.append(
[
(x, y + draw_hatch / 2 + draw_hatch / 4),
(x + draw_hatch, y + draw_hatch / 2 + draw_hatch / 4),
]
) # horizontal lines with additional offset
diagonal_lines.append(
[(x + draw_hatch, y), (x, y + draw_hatch)]
) # diagonal lines, left
t1 = time.time()
print("Wrangling points...")
# Make segments into lines
line_groups = [horizontal_lines, diagonal_lines]
for line_group in line_groups:
line_group = [
[
(
line1 + line2[1:]
if line1 and line2 and line1[-1] == line2[0]
else line1
)
for line1, line2 in zip(line_group, line_group[1:])
]
for _ in range(len(line_group))
]
# in each line group keep any non-empty lines
saved_lines = [[line[0], line[-1]] for line in line_group if line]
line_group.clear()
line_group.extend(saved_lines)
lines = [item for group in line_groups for item in group]
t2 = time.time()
print(f"Hatching: {t1 - t0}")
print(f"Wrangling: {t2 - t1}")
print(f"Total: {t2 - t0}")
return lines
# -------------- supporting functions for drawing contours --------------
def find_edges(image):
print("Finding edges...")
if NO_CV_MODE:
apply_mask(image, [F_SOBEL_X, F_SOBEL_Y])
else:
im = np.array(image)
im = cv2.GaussianBlur(im, (3, 3), 0)
im = cv2.Canny(im, 100, 200)
image = Image.fromarray(im)
return image.point(lambda p: p > 128 and 255)
def get_dots(image):
print("Getting contour points...")
h, w = image.shape
dots = []
for y in range(h - 1):
row = []
for x in range(1, w):
if image[y, x] == 255:
if row and x - row[-1][0] == row[-1][1] + 1:
row[-1] = (row[-1][0], row[-1][1] + 1)
else:
row.append((x, 0))
dots.append(row)
return dots
def connect_dots(dots):
print("Connecting contour points...")
contours = []
for y, row in enumerate(dots):
for x, v in row:
if v > -1:
if y == 0:
contours.append([(x, y)])
else:
closest, closest_dist = min(
((x0, v0) for x0, v0 in dots[y - 1]),
key=lambda point: abs(point[0] - x),
default=(None, None),
)
if closest is None or abs(closest - x) > 3:
contours.append([(x, y)])
else:
for contour in contours:
if contour[-1] == (closest, y - 1):
contour.append((x, y))
break
else:
contours.append([(x, y)])
return contours
# -------------- optimisation for pen movement --------------
def sort_lines(lines):
print("Optimizing stroke sequence...")
sorted_lines = [lines.pop(0)]
while lines:
last_point = sorted_lines[-1][-1]
closest_line = min(
lines,
key=lambda line: min(
distance_sum(line[0], last_point), distance_sum(line[-1], last_point)
),
)
if distance_sum(closest_line[0], last_point) > distance_sum(
closest_line[-1], last_point
):
closest_line.reverse()
sorted_lines.append(closest_line)
lines.remove(closest_line)
return sorted_lines
def lines_to_file(lines, filename):
with open(filename, "w") as file_to_save:
json.dump(lines, file_to_save, indent=4)
# -------------- helper functions --------------
def mid_point(*args):
xs, ys = 0, 0
for p in args:
xs += p[0]
ys += p[1]
return xs / len(args), ys / len(args)
def distance_sum(*points):
return sum(
math.hypot(points[i][0] - points[i - 1][0], points[i][1] - points[i - 1][1])
for i in range(1, len(points))
)
# -------------- code used when open CV is not available --------------
def apply_mask(IM, masks):
px = IM.load()
w, h = IM.size
npx = {}
for x in range(0, w):
for y in range(0, h):
a = [0] * len(masks)
for i in range(len(masks)):
for p in masks[i].keys():
if 0 < x + p[0] < w and 0 < y + p[1] < h:
a[i] += px[x + p[0], y + p[1]] * masks[i][p]
if sum(masks[i].values()) != 0:
a[i] = a[i] / sum(masks[i].values())
npx[x, y] = int(sum([v**2 for v in a]) ** 0.5)
for x in range(0, w):
for y in range(0, h):
px[x, y] = npx[x, y]
# Constants for masking
F_BLUR = {
(-2, -2): 2,
(-1, -2): 4,
(0, -2): 5,
(1, -2): 4,
(2, -2): 2,
(-2, -1): 4,
(-1, -1): 9,
(0, -1): 12,
(1, -1): 9,
(2, -1): 4,
(-2, 0): 5,
(-1, 0): 12,
(0, 0): 15,
(1, 0): 12,
(2, 0): 5,
(-2, 1): 4,
(-1, 1): 9,
(0, 1): 12,
(1, 1): 9,
(2, 1): 4,
(-2, 2): 2,
(-1, 2): 4,
(0, 2): 5,
(1, 2): 4,
(2, 2): 2,
}
F_SOBEL_X = {
(-1, -1): 1,
(0, -1): 0,
(1, -1): -1,
(-1, 0): 2,
(0, 0): 0,
(1, 0): -2,
(-1, 1): 1,
(0, 1): 0,
(1, 1): -1,
}
F_SOBEL_Y = {
(-1, -1): 1,
(0, -1): 2,
(1, -1): 1,
(-1, 0): 0,
(0, 0): 0,
(1, 0): 0,
(-1, 1): -1,
(0, 1): -2,
(1, 1): -1,
}