-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndarrays_to_str.py
189 lines (165 loc) · 6.22 KB
/
ndarrays_to_str.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
import time
import numpy as np
import random
# from threading import Thread
from code_source import code_source
from codes_style import codes_style
from code_source import one_color_codes
class ndarrays_to_str:
"""
将单张图像转换为str
"""
def __init__(self, code_source_obj: code_source):
"""
指定字符集生成转换器
:param code_source_obj: 字符集对象
"""
self.codes_dict: dict = code_source_obj.code_dict
self.color_dict: dict = code_source_obj.color_dict
self.is_colorful = True
self.trans_func = self._gray_codes_no_reverse
self.img = None
self.img_shape = None
self.out_str = ''
def img_to_str(self, image: np.ndarray, img_shape: tuple, style: codes_style):
self.img = image
self.img_shape = img_shape
self.is_colorful = len(image.shape) == 3
if style.color:
if style.codes:
self.trans_func = self._color_codes
else:
self.trans_func = self._color_block
else:
if style.codes:
if style.reverse:
self.trans_func = self._gray_codes_reverse
else:
self.trans_func = self._gray_codes_no_reverse
else:
self.trans_func = self._gray_block
self.out_str = '[38;2;255;255;255;48;2;0;0;0m'
self._img_to_str_()
return self.out_str
def _img_to_str_old_(self):
"""
将单帧图像按设定风格转换为字符串
风格默认为灰色无反转字符
速度过慢已启用
"""
t_s = time.time()
for x in range(self.img_shape[0]):
for y in range(self.img_shape[1]):
self.out_str += self.trans_func(self.img[x, y])
self.out_str += '[0m\n'
# print('merge', time.time() - t_s)
def _img_to_str_(self):
"""
将单帧图像按设定风格转换为字符串
风格默认为灰色无反转字符
将每行分配到不同线程
"""
pix_str_ndarray = ['' for _ in range(self.img_shape[0])]
t_s = time.time()
for x in range(self.img_shape[0]):
self._line_pix_to_str(x, pix_str_ndarray)
# Thread(target=self._line_pix_to_str, args=(x, pix_str_ndarray)).start()
for x in range(self.img_shape[0]):
self.out_str += pix_str_ndarray[x]
# print('merge', '{} frame/s'.format(round(1 / (time.time() - t_s), 1)))
def _line_pix_to_str(self, x, str_array):
"""
多线程单行处理函数
:param x: 行数
:param str_array: 每行str储存list
"""
line = ''
for y in range(self.img_shape[1]):
line += self.trans_func(self.img[x, y])
# str_array[x] = line + '[0m\n'
str_array[x] = line + '\n'
def _gray_codes_reverse(self, pix: np.ndarray) -> str:
out_str = ''
if self.is_colorful:
pix = int(np.average(pix))
is_reverse = pix > 127
if is_reverse:
pix = 255 - pix
out_str = '[38;2;0;0;0;48;2;255;255;255m'
else:
out_str = '[38;2;255;255;255;48;2;0;0;0m'
pix = self.color_dict[pix]
find_strs: one_color_codes = self.codes_dict[pix]
out_str += find_strs.codes[random.Random().randint(0, find_strs.codes_num - 1)]
return out_str
def _gray_block(self, pix: np.ndarray) -> str:
out_str = ''
if self.is_colorful:
pix = int(np.average(pix))
pix = int(pix)
out_str = '[48;2;{0};{0};{0}m '.format(pix)
return out_str
def _gray_codes_no_reverse(self, pix: np.ndarray) -> str:
out_str = ''
if self.is_colorful:
pix = int(np.average(pix))
pix = int(pix / 2)
pix = self.color_dict[pix]
find_strs: one_color_codes = self.codes_dict[pix]
out_str = find_strs.codes[random.Random().randint(0, find_strs.codes_num - 1)]
return out_str
def _color_block(self, pix: np.ndarray) -> str:
out_str = ''
if self.is_colorful:
out_str = '[48;2;{0};{1};{2}m '.format(pix[0], pix[1], pix[2])
else:
out_str = '[48;2;{0};{0};{0}m '.format(pix[0])
return out_str
def _color_codes(self, pix: np.ndarray) -> str:
out_str = ''
r = g = b = 0
r_code = g_code = b_code = 0
r_bg = g_bg = b_bg = 0
if self.is_colorful:
r = pix[0]
g = pix[1]
b = pix[2]
# print(r,g,b)
else:
r = g = b = pix[0]
max_dif = max([abs(127 - r), abs(127 - g), abs(127 - b)])
# print('max_dif ', max_dif)
max_dif = self.color_dict[max_dif]
if r > max_dif:
r_code = 255
r_bg = round((r - max_dif) * (255.0 / (255.0 - float(max_dif))))
else:
r_code = round(r * (255.0 / max_dif))
r_bg = 0
if g > max_dif:
g_code = 255
g_bg = round((g - max_dif) * (255.0 / (255.0 - float(max_dif))))
else:
g_code = round(g * (255.0 / max_dif))
g_bg = 0
if b > max_dif:
b_code = 255
b_bg = round((b - max_dif) * (255.0 / (255.0 - float(max_dif))))
else:
b_code = round(b * (255.0 / max_dif))
b_bg = 0
out_str = '[38;2;{0};{1};{2};48;2;{3};{4};{5}m'.format(r_code, g_code, b_code, r_bg, g_bg, b_bg)
find_strs: one_color_codes = self.codes_dict[max_dif]
out_str += find_strs.codes[random.Random().randint(0, find_strs.codes_num - 1)]
return out_str
if __name__ == '__main__':
code_source = code_source('./xin_song_ti_16/code_sources_v3.txt',
'./xin_song_ti_16/code_sources_v3_img_xinsongti_16.png',
16,
'新宋体'
)
convert = ndarrays_to_str(code_source)
im_shape = (120, 67, 3)
img = np.random.randint(0, 100, size=im_shape)
a = convert.img_to_str(img, im_shape, codes_style(color=True))
b = convert.img_to_str(img, im_shape, codes_style(color=True, codes=False))