Repository navigation
Expand file tree
/
Copy pathperspective_python.py
More file actions
713 lines (541 loc) · 14.7 KB
/
Copy pathperspective_python.py
File metadata and controls
713 lines (541 loc) · 14.7 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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
import numpy as np
import math
# ============================================================
# 作品の条件
# ============================================================
# A3横向きの加工範囲
PANEL_WIDTH_MM = 420.0
PANEL_HEIGHT_MM = 297.0
# 4px/mm
# 1枚の出力サイズ:1680 × 1188px
PIXELS_PER_MM = 4.0
# アクリル板
LAYER_COUNT = 3
LAYER_GAP_MM = 20.0
# グラフィティが現れる視点
VIEW_ANGLE_DEG = 45.0
# 左45°で合わないときは -1 に変更
SHIFT_DIRECTION = 1
# ============================================================
# 正面から見える縦線
# ============================================================
# 縦線の太さ
# 手作業を考えて4mmに設定
LINE_WIDTH_MM = 4.0
# 縦線の中心から次の縦線の中心まで
# 数字を大きくすると線が少なくなる
LINE_PITCH_MM = 14.0
# A3用紙の周囲に残す余白
MARGIN_X_MM = 10.0
MARGIN_Y_MM = 10.0
# True:縦線を全体に入れる
USE_BASE_VERTICAL_LINES = True
# 1.0:全ての縦線を使う
# 0.8:縦線を一部減らす
BASE_LINE_DENSITY = 1.0
VERTICAL_CELL_MM = 20.0
GRAFFITI_THRESHOLD = 0.22
# グラフィティ部分の縦断片の太さ
GRAFFITI_LINE_WIDTH_MM = 6.0
# 1:20mm程度の短い断片も残す
# 2:40mm以上の断片を中心に残す
MINIMUM_RUN_CELLS = 1
BLACK_GRAFFITI_ON_WHITE = True
# 白と黒を判定する値
IMAGE_THRESHOLD = 0.5
# ============================================================
# TouchDesignerのコールバック
# ============================================================
def onSetupParameters(scriptOp):
return
def onPulse(par):
return
# ============================================================
# 補助関数
# ============================================================
def resize_nearest(image, new_height, new_width):
"""
NumPyのみで画像を最近傍リサイズする。
輪郭をぼかさず白黒を維持する。
"""
old_height, old_width = image.shape
if old_height < 1 or old_width < 1:
return np.zeros(
(new_height, new_width),
dtype=np.float32
)
y_indices = np.linspace(
0,
old_height - 1,
new_height
).astype(np.int32)
x_indices = np.linspace(
0,
old_width - 1,
new_width
).astype(np.int32)
return image[
y_indices[:, None],
x_indices[None, :]
]
def fit_image_to_canvas(
source_mask,
canvas_height,
canvas_width,
margin_x,
margin_y,
maximum_shift
):
"""
入力グラフィティの縦横比を維持しながら、
A3横の加工範囲内へ配置する。
"""
source_height, source_width = source_mask.shape
available_width = (
canvas_width
- margin_x * 2
- maximum_shift
)
available_height = (
canvas_height
- margin_y * 2
)
available_width = max(1, available_width)
available_height = max(1, available_height)
scale = min(
available_width / source_width,
available_height / source_height
)
new_width = max(
1,
int(round(source_width * scale))
)
new_height = max(
1,
int(round(source_height * scale))
)
resized = resize_nearest(
source_mask.astype(np.float32),
new_height,
new_width
)
resized = resized >= 0.5
canvas = np.zeros(
(canvas_height, canvas_width),
dtype=bool
)
# 45°補正による左右移動を考慮して中央に置く
x_start = (
canvas_width - new_width
) // 2
y_start = (
canvas_height - new_height
) // 2
x_end = min(
canvas_width,
x_start + new_width
)
y_end = min(
canvas_height,
y_start + new_height
)
copy_width = x_end - x_start
copy_height = y_end - y_start
if copy_width > 0 and copy_height > 0:
canvas[
y_start:y_end,
x_start:x_end
] = resized[
:copy_height,
:copy_width
]
return canvas
def draw_vertical_rectangle(
mask,
center_x,
y_start,
y_end,
rectangle_width
):
"""
マスク上に縦長の長方形を描く。
"""
canvas_height, canvas_width = mask.shape
half_width = rectangle_width // 2
x_start = max(
0,
center_x - half_width
)
x_end = min(
canvas_width,
center_x + half_width + 1
)
y_start = max(
0,
int(y_start)
)
y_end = min(
canvas_height,
int(y_end)
)
if x_end > x_start and y_end > y_start:
mask[
y_start:y_end,
x_start:x_end
] = True
def remove_short_runs(values, minimum_cells):
"""
短すぎる孤立したグラフィティ断片を削除する。
"""
if minimum_cells <= 1:
return values.copy()
result = values.copy()
value_count = len(values)
position = 0
while position < value_count:
if not values[position]:
position += 1
continue
run_start = position
while (
position < value_count
and values[position]
):
position += 1
run_end = position
run_length = run_end - run_start
if run_length < minimum_cells:
result[
run_start:run_end
] = False
return result
def mask_to_rgba(mask):
"""
Trueの部分を黒、
Falseの部分を白にして印刷画像を作る。
"""
height, width = mask.shape
output = np.ones(
(height, width, 4),
dtype=np.float32
)
black_value = np.where(
mask,
0.0,
1.0
)
output[:, :, 0] = black_value
output[:, :, 1] = black_value
output[:, :, 2] = black_value
output[:, :, 3] = 1.0
return output
def add_layer_mark(image, layer_number):
"""
左上に黒い四角を配置して、
Layer 1・2・3を見分けられるようにする。
"""
mark_size = max(
4,
int(round(
3.0 * PIXELS_PER_MM
))
)
mark_gap = max(
2,
int(round(
2.0 * PIXELS_PER_MM
))
)
start_x = int(round(
3.0 * PIXELS_PER_MM
))
start_y = int(round(
3.0 * PIXELS_PER_MM
))
for index in range(layer_number):
x_start = (
start_x
+ index * (mark_size + mark_gap)
)
x_end = x_start + mark_size
y_end = start_y + mark_size
image[
start_y:y_end,
x_start:x_end,
0:3
] = 0.0
return image
# ============================================================
# メイン処理
# ============================================================
def onCook(scriptOp):
# 入力TOPがない場合
if len(scriptOp.inputs) == 0:
scriptOp.clear()
return
try:
source = scriptOp.inputs[0].numpyArray(
delayed=False
)
except Exception:
scriptOp.clear()
return
if source is None:
scriptOp.clear()
return
# --------------------------------------------------------
# A3出力サイズ
# --------------------------------------------------------
panel_width_px = int(round(
PANEL_WIDTH_MM * PIXELS_PER_MM
))
panel_height_px = int(round(
PANEL_HEIGHT_MM * PIXELS_PER_MM
))
margin_x_px = int(round(
MARGIN_X_MM * PIXELS_PER_MM
))
margin_y_px = int(round(
MARGIN_Y_MM * PIXELS_PER_MM
))
line_width_px = max(
2,
int(round(
LINE_WIDTH_MM
* PIXELS_PER_MM
))
)
graffiti_line_width_px = max(
line_width_px,
int(round(
GRAFFITI_LINE_WIDTH_MM
* PIXELS_PER_MM
))
)
line_pitch_px = max(
line_width_px + 1,
int(round(
LINE_PITCH_MM
* PIXELS_PER_MM
))
)
vertical_cell_px = max(
1,
int(round(
VERTICAL_CELL_MM
* PIXELS_PER_MM
))
)
# --------------------------------------------------------
# 45°から見た際のレイヤーごとの横ずれ
# --------------------------------------------------------
angle_radians = math.radians(
VIEW_ANGLE_DEG
)
shift_per_layer_mm = (
LAYER_GAP_MM
* math.tan(angle_radians)
)
shift_per_layer_px = int(round(
shift_per_layer_mm
* PIXELS_PER_MM
))
maximum_shift_px = (
shift_per_layer_px
* (LAYER_COUNT - 1)
)
# --------------------------------------------------------
# 入力画像をグレースケールにする
# --------------------------------------------------------
if (
source.ndim == 3
and source.shape[2] >= 3
):
grayscale = (
source[:, :, 0] * 0.299
+ source[:, :, 1] * 0.587
+ source[:, :, 2] * 0.114
)
elif source.ndim == 3:
grayscale = source[:, :, 0]
else:
grayscale = source
# TouchDesignerと印刷画像の上下方向を合わせる
grayscale = np.flipud(grayscale)
# --------------------------------------------------------
# 白背景・黒いグラフィティを抽出
# --------------------------------------------------------
if BLACK_GRAFFITI_ON_WHITE:
# 暗い部分をグラフィティとして認識
source_mask = (
grayscale < IMAGE_THRESHOLD
)
else:
# 白い部分をグラフィティとして認識
source_mask = (
grayscale > IMAGE_THRESHOLD
)
# --------------------------------------------------------
# グラフィティをA3範囲へ収める
# --------------------------------------------------------
target_mask = fit_image_to_canvas(
source_mask,
panel_height_px,
panel_width_px,
margin_x_px,
margin_y_px,
maximum_shift_px
)
# Layer 1〜3用の空画像
layers = [
np.zeros(
(
panel_height_px,
panel_width_px
),
dtype=bool
)
for _ in range(LAYER_COUNT)
]
# 縦線の位置
x_positions = list(range(
margin_x_px,
panel_width_px - margin_x_px,
line_pitch_px
))
# --------------------------------------------------------
# 正面用の縦線を3枚へ順番に配置
# --------------------------------------------------------
if USE_BASE_VERTICAL_LINES:
for stripe_index, x_position in enumerate(
x_positions
):
layer_index = (
stripe_index % LAYER_COUNT
)
# 決まった規則で一部を間引く
density_value = (
(stripe_index * 37) % 100
) / 100.0
if density_value <= BASE_LINE_DENSITY:
draw_vertical_rectangle(
layers[layer_index],
x_position,
margin_y_px,
panel_height_px - margin_y_px,
line_width_px
)
# --------------------------------------------------------
# グラフィティを縦長の断片へ変換
# --------------------------------------------------------
cell_count_y = int(math.ceil(
panel_height_px
/ vertical_cell_px
))
for stripe_index, target_x in enumerate(
x_positions
):
cell_values = np.zeros(
cell_count_y,
dtype=bool
)
# その縦線周辺のグラフィティを調べる
sample_half_width = max(
line_pitch_px // 2,
graffiti_line_width_px
)
x_start = max(
0,
target_x - sample_half_width
)
x_end = min(
panel_width_px,
target_x + sample_half_width
)
for cell_y in range(cell_count_y):
y_start = (
cell_y * vertical_cell_px
)
y_end = min(
panel_height_px,
y_start + vertical_cell_px
)
sample = target_mask[
y_start:y_end,
x_start:x_end
]
if sample.size > 0:
black_ratio = float(
np.mean(sample)
)
else:
black_ratio = 0.0
if (
black_ratio
>= GRAFFITI_THRESHOLD
):
cell_values[cell_y] = True
cell_values = remove_short_runs(
cell_values,
MINIMUM_RUN_CELLS
)
# ----------------------------------------------------
# グラフィティ断片を3層へ分散
# ----------------------------------------------------
for cell_y, is_active in enumerate(
cell_values
):
if not is_active:
continue
# 上下のセルごとに担当レイヤーを変える
layer_index = (
stripe_index + cell_y
) % LAYER_COUNT
y_start = (
cell_y * vertical_cell_px
)
y_end = min(
panel_height_px,
y_start + vertical_cell_px
)
# 20mm間隔・45°に合わせた逆方向補正
shift_amount = int(round(
layer_index
* shift_per_layer_px
* SHIFT_DIRECTION
))
print_x = (
target_x - shift_amount
)
draw_vertical_rectangle(
layers[layer_index],
print_x,
y_start,
y_end,
graffiti_line_width_px
)
# --------------------------------------------------------
# Layer 1〜3を横並びにして出力
# --------------------------------------------------------
layer_images = []
for layer_index, layer_mask in enumerate(
layers
):
layer_image = mask_to_rgba(
layer_mask
)
layer_image = add_layer_mark(
layer_image,
layer_index + 1
)
layer_images.append(
layer_image
)
output = np.concatenate(
layer_images,
axis=1
).astype(np.float32)
# TouchDesigner表示用に上下を戻す
output = np.flipud(output)
scriptOp.copyNumpyArray(output)
return