-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
139 lines (93 loc) · 4.55 KB
/
draw.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 9 21:07:57 2020
@author: ivan
"""
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
from initSettings import cf
import cv2
orientations = [30, 150, 60, 120, 15, 165, 105, 75, 135]
def HEX2RGB(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
colors_hex = list(colors.TABLEAU_COLORS.values())
colors = [HEX2RGB(el) for el in colors_hex]
def draw_pattern(img, masks, idx_sets):
dd = cf.pat_dist
out = img.copy()
Y, X, Z = img.shape
print('X: {}; Y: {}'.format(X, Y))
masks = np.array(masks)
for cur_idx_set in range(len(idx_sets)):
col = colors[cur_idx_set]
pattern = np.zeros_like(img)
cur_set = list(idx_sets[cur_idx_set])
cur_mask = np.uint8(np.sum(masks[cur_set], axis=0)/255)
(i1, i2, i3) = cur_set
ori = orientations[cur_idx_set]
col = colors[cur_idx_set]
theta = ori *np.pi / 180
csc = 1/np.sin(theta)
cot = 1/np.tan(theta)
z1 = 0
if ori < 90:
while True:
pt1 = (X - z1*dd*csc/cot, 0)
pt2 = (X, z1*dd*csc)
if pt1[0] <= 0: pt1 = (0, -X*cot + z1*dd*csc)
if pt2[1] >= Y: pt2 = (X + (Y - z1*dd*csc)/cot, Y)
if pt1[1] > Y: break
pt1 = (int(np.round(pt1[0])), int(np.round(Y-pt1[1])))
pt2 = (int(np.round(pt2[0])), int(np.round(Y-pt2[1])))
cv2.line(pattern, pt1, pt2, col, cf.pat_thick)
z1+=1
elif ori > 90:
while True:
pt1 = (0, z1*dd*csc)
pt2 = (z1*dd*csc/cot, 0)
if pt1[1] >= Y: pt1 = ((Y-z1*dd*csc)/cot, Y)
if pt2[0] <= 0: pt2 = (X, X*cot + z1*dd*csc)
if pt2[1] > Y: break
pt1 = (int(np.round(pt1[0])), int(np.round(Y-pt1[1])))
pt2 = (int(np.round(pt2[0])), int(np.round(Y-pt2[1])))
cv2.line(pattern, pt1, pt2, col, cf.pat_thick)
z1+=1
rep_mask = np.dstack((cur_mask,)*3)
masked_pattern = pattern * rep_mask
# =============================================================================
# plt.subplots()
# plt.subplot(311)
# plt.imshow(pattern)
# plt.subplot(312)
# plt.imshow(rep_mask)
# plt.subplot(313)
# plt.imshow(masked_pattern)
#
# plt.imshow(out)
# =============================================================================
out = np.where(masked_pattern != 0, masked_pattern, out)
return out
def put_text(img_rgb, board, boxes):
from PIL import Image, ImageFont, ImageDraw
from sys import platform
############################################################################ PIL #####
img_pil = Image.fromarray((img_rgb).astype(np.uint8)) ##
draw = ImageDraw.Draw(img_pil) ##
if platform == "linux" or platform == "linux2": ##
font = ImageFont.truetype(cf.font_path_lin, cf.font_size) ##
elif platform == "darwin": ##
raise NotImplementedError ##
elif platform == "win32": ##
font = ImageFont.truetype(cf.font_path_win, cf.font_size) ##
######################################################################################
for card, box in zip(board, boxes):
############################################################ PIL ###############
draw.text((box[0, 0], box[0, 1]), card.chars(), cf.font_color, font=font) #
################################################################################
return np.array(img_pil)
#output = draw_pattern(np.zeros((2000, 1500, 3)), None, [(1,2,3)])
#plt.imshow(output)