-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage2HTML.py
More file actions
80 lines (69 loc) · 2.31 KB
/
image2HTML.py
File metadata and controls
80 lines (69 loc) · 2.31 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
#!/usr/bin/env python3
import sys, getopt
from PIL import Image
from itertools import groupby
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print ('image2HTML.py -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('image2HTML.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
# Read Image
im = Image.open(inputfile).convert('RGB')
width, height = im.size
pixels = im.load()
# Write file
with open(outputfile,'w') as f:
f.write(process(width, height, pixels))
# RGB to HEX color
def tup2hex(rgb_tup):
t2h = '#%02x%02x%02x' % rgb_tup
if t2h[1:].count(t2h[1]) == 6:
return t2h[:4]
else:
return t2h
# Slice pixels into rows
def slice_it(hex_pixels, height):
rows_list = []
start = 0
for i in range(height):
stop = start + len(hex_pixels[i::height])
rows_list.append(hex_pixels[start:stop])
start = stop
return rows_list
# Process image data -> html
def process(width, height, pixels):
output = f'''<html><head></head><style>td{{height:1;padding:0;}}
table{{border-spacing:0;width:{width}}}</style><center>
<table><tr>'''
# Convert all pixels to hex format
hex_pixels = [tup2hex(pixels[x,y]) for y in range(height) for x in range(width)]
# Group the consecutive pixels with
# the same color in the same row into single html statement
# e.g. instead of multiple <td width=1 bgcolor=#000 /> make
# one with higher width <td width=100 bgcolor=#000 />
output_list = []
for l in slice_it(hex_pixels, height):
tpl = [(k, sum(1 for i in g)) for k,g in groupby(l)]
output_list.extend(tpl)
# Convert to HTML
count = 0
for d in output_list:
output += f'<td width={d[1]} bgcolor={d[0]} />'
count += d[1]
if count % width == 0:
output += '</tr></table><table><tr>'
output += '</tr></table></center></html>'
return output
if __name__ == "__main__":
main(sys.argv[1:])