img2html实例
img2html 是在知乎上看到的一个关于图像处理的开源包,是一个基于Python的程序,暂时不知道有什么用,不过看着挺好玩的,通过引用这这包实现一个小程序,和大家分享一下。
img2html 实现了把一张图片转换为html文件(也可以理解为转换为一个txt文档),但是文档依然可以看出是照片,让我想起一些个性的海报是不是这样做的
一张周董的照片,是不是很酷
html页面截图
html源码
<html>
<head>
<meta charset="utf-8">
<title>img2html by xlzd</title>
<style type="text/css">
body {
margin: 0px; padding: 0px; line-height:100%; letter-spacing:0px; text-align: center;
min-width: 1440px;
width: auto !important;
font-size: 10px;
background-color: ##000000;
font-family: monospace;
}
</style>
</head>
<body>
<div>
<font color="#b3b3b3">0</font><font color="#b3b3b3">1</font><font color="#b2b2b2">0</font><font color="#b2b2b2">1</font><font color="#b3b3b3">0</font><font color="#b4b4b4">1</font><font color="#b5b5b5">0</font><font color="#b6b6b6">1</font><font color="#b5b5b5">0</font><font color="#b5b5b5">1</font><font color="#b5b5b5">0</font><font color="#b4b4b4">1</font><font color="#b6b6b6">0</font><font color="#b6b6b6">1</font><font color="#b5b5b5">0</font><font color="#b6b6b6">1</font><font color="#b7b7b7">0</font><font color="#b7b7b7">1</font><font color="#b7b7b7">0</font><font color="#b7b7b7">1</font><font color="#b6b6b6">0</font><font color="#b7b7b7">1</font><font color="#b6b6b6">0</font><font color="#b5b5b5">1</font><font color="#b7b7b7">0</font><font color="#b6b6b6">1</font><font color="#b5b5b5">0</font><font color="#a4a4a4">1</font><font color="#585858">0</font><font color="#333333">1</font><font color="#262626">0</font><font color="#2c2c2c">1</font><font color="#414141">0</font><font color="#3a3a3a">1</font><font color="#373737">0</font><font color="#252525">1</font><font color="#080808">0</font><font color="#030303">1</font><font color="#010101">0</font><font color="#010101">1</font><font color="#040404">0</font><font color="#090909">1</font><font color="#212121">0</font><font color="#484848">1</font><font color="#3b3b3b">0</font><font color="#929292">1</font><font color="#b1b1b1">0</font><font color="#b2b2b2">1</font><font color="#b5b5b5">0</font><font color="#b5b5b5">1</font><font color="#b6b6b6">0</font><font color="#b6b6b6">1</font><font color="#b4b4b4">0</font><font color="#b1b1b1">1</font><font color="#b2b2b2">0</font><font color="#b1b1b1">1</font><font color="#b1b1b1">0</font><font color="#b0b0b0">1</font><font color="#b1b1b1">0</font><font color="#b1b1b1">1</font><font color="#b0b0b0">0</font><font color="#b2b2b2">1</font><font color="#b2b2b2">0</font><font color="#b2b2b2">1</font><font color="#b0b0b0">0</font><font color="#afafaf">1</font><font color="#afafaf">0</font><font color="#b0b0b0">1</font><font color="#b2b2b2">0</font><font color="#b2b2b2">1</font><font color="#b3b3b3">0</font><font color="#b1b1b1">1</font>
<br>
</div>
</body>
</html>
实现步骤:
1.在cmd下输入 pip install img2html
2.新建Python文件,引用这个包处理图片
具体Python代码如下:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from img2html.converter import Img2HTMLConverter
def saveHtml(file_name, file_content):
# 注意windows文件命名的禁用符,比如 /
with open(file_name.replace('/', '_') + ".html", "wb") as f:
# 写文件用bytes而不是str,所以要转码
f.write(file_content)
converter = Img2HTMLConverter()
html = converter.convert('C:\Users\lele\Desktop\\5.jpg')#图片路径设#为自己的路径。
saveHtml("text1", html)#保存html页面。
一下是转换的具体代码,我们可以修改一些颜色,字体大小,字样。以达到我们想要的效果。原文是显示一个汉字,我改为了0,1,就是用0,1去显示一张照片,因为之前看到图灵系列书的起始页有类似的插图。
#!/usr/bin/env python
# encoding=utf-8
from __future__ import print_function, unicode_literals
from collections import namedtuple
from itertools import cycle
import jinja2
from PIL import Image
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>
</body>
</html>'''
class Img2HTMLConverter(object):
def __init__(self,
font_size=10,
char='01010101',
background='#000000',
title='img2html by xlzd',
font_family='monospace'):
self.font_size = font_size
self.background = background
self.title = title
self.font_family = font_family
self.char = cycle(char)
def convert(self, source):
image = Image.open(source)
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)
for col in xrange(col_blocks):
render_group = RenderGroup()
for row in xrange(row_blocks):
pixels = []
for y in xrange(self.font_size):
for x in xrange(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=self.char.next())
render_group.append(render_item)
progress += step
print('\rprogress: {:.2f}%'.format(progress * 100), end='')
html_image.append(render_group)
return self.render(html_image)
def render(self, html_image):
template = jinja2.Template(TEMPLATE)
return template.render(
html_image=html_image,
size=self.font_size,
background=self.background,
title=self.title,
font_family=self.font_family,
width=self.font_size * len(html_image[0]) * 2
)
@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)),
)