从零学python系列之教你如何根据图片生成字符画

WBOY
Release: 2016-06-16 08:44:08
Original
1246 people have browsed it

说下思路吧:

原图->灰度->根据像素亮度-映射到指定的字符序列中->输出。
字符越多,字符变化稠密。效果会更好。
如果根据灰度图的像素亮度范围制作字符画,效果会更好。
如果再使用调色板,对字符进行改色,就更像原图了。

这是原图: 

从零学python系列之教你如何根据图片生成字符画

这是生成的字符画:

从零学python系列之教你如何根据图片生成字符画

废话不多说,直接上代码:

复制代码 代码如下:

import Image
chars =" ...',;:clodxkLO0DGEKNWMM"
fn=r'c:\users\liabc\desktop\jianbing.png'
f1=lambda F:''.join([(k%100!=0) and m or m+'\n' for k,m in enumerate(apply(lambda x:[chars[x[j,i]%len(chars)] for i in xrange(70) for j in xrange(100)],(Image.open(F).resize((100,70)).convert("L").load(),)),1)])
 f=open(r"c:\users\liabc\desktop\aface.txt","w")
f.write(f1(fn))
f.close()
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!