-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconverter.py
227 lines (205 loc) · 7.37 KB
/
converter.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
#!/usr/bin/env python
# encoding=utf-8
from __future__ import print_function, unicode_literals
import os
import sys
import time
import json
from collections import namedtuple
from itertools import cycle
import jinja2
from PIL import Image
import numpy as np
Point = namedtuple('Point', ['x', 'y'])
Pixel = namedtuple('Pixel', ['r', 'g', 'b'])
RenderItem = namedtuple('RenderItem', ['color', 'char'])
RenderGroup = list
HTMLImage = list
TEMPLATE = '''
<html>
<head>
<meta charset="utf-8">
<title>{{ title }}</title>
<style type="text/css">
body {
margin: 0px; padding: 0px; line-height:100%; letter-spacing:0px; text-align: center;
min-width: {{width}}px;
width: auto !important;
font-size: {{size}}px;
background-color: #{{background}};
font-family: {{font_family}};
}
</style>
</head>
<body>
<div>
{% for group in html_image %}
{% for item in group %}<font color="{{ item.color }}">{{ item.char }}</font>{% endfor %}
<br>
{% endfor %}
</div>
<script>
var colors = {{colors}}
let i =-1;
var fonts = document.getElementsByTagName('font')
console.log('fonts.length:'+fonts.length);
var int = setInterval(function(){
if(i==colors.length-1){
i=-1;
console.log('播放完成');
}
i++;
let color = colors[i];
//console.log('color.type'+typeof(color));
//console.log('color.length'+color.length)
for(var j=0;j<color.length;j++){
fonts[j].color = color[j];
}
},300)
</script>
</body>
</html>'''
_c = cycle(r'/-\|')
def _progress_callback(percent):
pass
# if percent == 100:
# # os.system("cls")
# print("100%完成!")
# else:
# lca = getattr(_progress_callback, '_last_call_at', 0)
# if time.time() - lca > 0.5:
# _progress_callback._last_call_at = time.time()
# #sys.stdout.write('\r{} progress: {:.2f}%'.format(_c.next(), percent))
#
# Img2HTMLConverter.showprogress(percent)
class Img2HTMLConverter(object):
def __init__(self,
font_size=10,
char='䦗',
background='#000000',
title='by xx',
font_family='monospace',
progress_callback=None):
self.font_size = font_size
self.background = background
self.title = title
self.font_family = font_family
if isinstance(char, str):
# char = char.encode('utf-8')
pass
self.char = cycle(char)
self._prg_cb = progress_callback or _progress_callback
def convert(self, source, colors):
image = Image.open(source)
width, height = image.size
# 按比例缩高
if width > 1200 and height < width:
height = int(1200 / width * height)
width = 1200
# 按比例缩宽
elif height > 800:
width = int(800 / height * width)
height = 800
size = width, height
image.thumbnail(size, Image.ANTIALIAS)
# print(width,height)
width, height = image.size
row_blocks = int(round(float(width) / self.font_size))
col_blocks = int(round(float(height) / self.font_size))
html_image = HTMLImage()
progress = 0.0
step = 1. / (col_blocks * row_blocks)
print(col_blocks, row_blocks)
for col in range(col_blocks):
render_group = RenderGroup()
for row in range(row_blocks):
pixels = []
for y in range(self.font_size):
for x in range(self.font_size):
point = Point(row * self.font_size + x, col * self.font_size + y)
if point.x >= width or point.y >= height:
continue
pixels.append(Pixel(*image.getpixel(point)[:3]))
average = self.get_average(pixels=pixels)
color = self.rgb2hex(average)
render_item = RenderItem(color='#' + color, char=next(self.char))
render_group.append(render_item)
progress += step
self._prg_cb(int(progress * 100))
html_image.append(render_group)
self._prg_cb(100)
return self.render(html_image, colors)
def convertNDArrayColors(self, ndarray1):
colors = []
height, width, pixels = ndarray1.shape
row_blocks = int(round(float(width) / self.font_size))
col_blocks = int(round(float(height) / self.font_size))
progress = 0.0
for col in range(col_blocks):
for row in range(row_blocks):
pixels = ndarray1[col * self.font_size:(col+1) * self.font_size, row * self.font_size:(row+1) * self.font_size]#.copy()
r, g, b = 0, 0, 0
for rowPixel in pixels:
for pixel in rowPixel:
r += int(pixel[2])
g += int(pixel[1])
b += int(pixel[0])
base = float(self.font_size * self.font_size)
pixel = Pixel(
r=int(round(r / base)),
g=int(round(g / base)),
b=int(round(b / base)),
)
color = self.rgb2hex(pixel)
colors.append('#' + color) # 将color存入数组里
self._prg_cb(int(progress * 100))
self._prg_cb(100)
return json.dumps(colors)
def convert(self, width, height, colors):
row_blocks = int(round(float(width) / self.font_size))
col_blocks = int(round(float(height) / self.font_size))
html_image = HTMLImage()
for col in range(col_blocks):
render_group = RenderGroup()
for row in range(row_blocks):
render_item = RenderItem(color=colors[0][col*row_blocks+row], char=next(self.char))
render_group.append(render_item)
html_image.append(render_group)
self._prg_cb(100)
return self.render(html_image, colors)
def showprogress(self, progress):
# os.system("cls")
# 打印一个#号,这种方法打印不会自动换行
# sys.stdout.write("转换中:"+str(progress)+'%')
print('\r转换中:{:.2%}'.format(progress), end='')
# 实时刷新一下,否则上面这一条语句,会等#号全部写入到缓存中后才一次性打印出来
sys.stdout.flush()
# 每个#号等待0.1秒的时间打印
# time.sleep(0.1)
def render(self, html_image, colors):
template = jinja2.Template(TEMPLATE)
return template.render(
html_image=html_image,
colors=colors,
size=self.font_size,
background=self.background,
title=self.title,
font_family=self.font_family,
width=self.font_size * len(html_image[0])
)
@staticmethod
def rgb2hex(pixel):
return '{:02x}{:02x}{:02x}'.format(*pixel)
@staticmethod
def get_average(pixels):
r, g, b = 0, 0, 0
for pixel in pixels:
r += pixel.r
g += pixel.g
b += pixel.b
base = float(len(pixels))
return Pixel(
r=int(round(r / base)),
g=int(round(g / base)),
b=int(round(b / base)),
)