用python图片转字符串
python实现图片转字符小工具本文实例为大家分享了python图片转字符小工具的具体实现代码,供大家参考,具体内容如下
|
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.jpg" alt="用python图片转字符串(python实现图片转字符小工具)" border="0" /> 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