本文实例为大家分享了python图片转字符小工具的具体实现代码,供大家参考,具体内容如下
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
|
from pil import image #灰度与字符的映射 ascii_char = list ( "$@b%8&wm#*oahkbdpqwmzo0qlcjuyxzcvunxrjft/\|()1{}[]?-_+~<>i!li;:,\"^`'. " ) width = int ( input ( "请输入你想输出的宽度:" )) height = int ( input ( "请输入你想输出的高度:" )) input = r 'd:\download\ascii_dora.png' output = r 'd:\download\output.txt' def get_char(r,g,b,alpha = 256 ): if alpha = = 0 : return ' ' gray = 0.2126 * r + 0.7152 * g + 0.0722 * b length = len (ascii_char) unit = ( 256 + 1 ) / length return ascii_char[ int (gray / unit)] im = image. open ( input ) im = im.resize((width,height),image.nearest) txt = '' for i in range (height): for j in range (width): txt + = get_char( * im.getpixel((j,i))) txt + = '\n' with open (output, 'w' ) as f: f.write(txt) |
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/ChenTianSaber/article/details/52280799