Python根据图片生成字符画
字符画很好玩,我们来看看怎样将一张图片变成字符画。
我们首先将图片变成黑白的,那么每个像素的取值范围为:0-255.
然后我们将0-255映射到0-14的范围上,然后用如下字符代替:
color = ‘MNHQ$OC?7>!:-;.’
也就是像素为0的点用“M”表示,像素为14的点用“.”表示。
原理非常的简单,我们用Python来编写的话也非常的简单。只要借助PIL,就可以很轻松地在Python中处理图像。
我们来看一段代码:
import Image
color = 'MNHQ$OC?7>!:-;.'
def to_html(func):
html_head = '''
<html>
<head>
<style type="text/css">
body {font-family:Monospace; font-size:5px;}
</style>
</head>
<body> '''
html_tail = '</body></html>'
def wrapper(img):
pic_str = func(img)
pic_str = ''.join(l + ' <br/>' for l in pic_str.splitlines())
return html_head + pic_str + html_tail
return wrapper
@to_html
def make_char_img(img):
pix = img.load()
pic_str = ''
width, height = img.size
for h in xrange(height):
for w in xrange(width):
pic_str += color[int(pix[w, h]) * 14 / 255]
pic_str += '\n'
return pic_str
def preprocess(img_name):
img = Image.open(img_name)
w, h = img.size
m = max(img.size)
delta = m / 200.0
w, h = int(w / delta), int(h / delta)
img = img.resize((w, h))
img = img.convert('L')
return img
def save_to_file(filename, pic_str):
outfile = open(filename, 'w')
outfile.write(pic_str)
outfile.close()
def main():
img = preprocess('6.jpg')
pic_str = make_char_img(img)
save_to_file('char.html', pic_str)
if __name__ == '__main__':
main()
整个程序的核心都在下面两行,一个是字符的色阶表,一个是映射公式。
color = 'MNHQ$OC?7>!:-;.'
pic_str += color[int(pix[w, h]) * 14 / 255]
效果如下:
原图:
字符画:
在线的图片字符画生成请见::1758/
源码:https://github.com/cedricporter/et-python/tree/master/web%20server/webpy

