python实现趣味图片字符化
作者:Good_Luck_Kevin2018 时间:2022-01-22 15:29:35
本文实例为大家分享了python实现趣味图片字符化的具体代码,供大家参考,具体内容如下
主要使用PIL库
先放效果图:
代码如下:
#coding=utf-8
from PIL import Image
ascii_char = list('MW$#@%&KERTYOJKLUIC{}*mnxgouic()<>\!~:;^·.')
def get_char(r,b,g,alpha = 256):
if alpha==0:
return ' '
gray = int(0.2126 * r + 0.7152 * g + 0.0722 *b)
unit = 256 / len(ascii_char)
return ascii_char[int(gray/unit)]
def main():
chage_image=input("请输入待转化照片的名字")
im = Image.open(chage_image)
width, height = im.width,im.height
#im = im.resize((width,height))
txt = "" #存放待写入的字符 使用NotePad+ 缩放观看
for i in range(height):
for j in range(width):
txt += get_char(*im.getpixel((j, i)))
txt += '\n'
input_file_name = chage_image[:-4]+".txt"
print(input_file_name)
with open(input_file_name,"w") as f:
f.write(txt)
print (input_file_name[:-4],"制作完成!")
if "__name__" =="__main__":
main()
其他类似这种制作的原理都是一样的,效果的差异,主要是对ascii_char列表里字符排列,
原则上,那些字符能给人视觉上占的面积大的排前面,占比少的排后面,列表元素越多,生成的图片细节越细腻。
来源:https://blog.csdn.net/Good_Luck_Kevin2018/article/details/80914746
标签:python,图片,字符化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
简单掌握Python的Collections模块中counter结构的用法
2023-05-17 00:20:13
解决python xlrd无法读取excel文件的问题
2022-01-25 10:48:44
让XML在ASP中发挥其长处
2008-01-16 19:07:00
百度在线手写输入法
2010-02-03 14:27:00
python通过opencv调用摄像头操作实例分析
2023-03-09 14:32:04
Oracle分析函数用法详解
2023-07-16 15:35:26
![](https://img.aspxhome.com/file/2023/4/64834_0s.png)
利用Python实现Excel的文件间的数据匹配功能
2023-12-04 19:53:42
![](https://img.aspxhome.com/file/2023/5/109845_0s.png)
使用pycharm将自己项目代码上传github(小白教程)
2022-05-02 20:37:06
![](https://img.aspxhome.com/file/2023/6/82526_0s.jpg)
PHP开发实现快递查询功能详解
2023-11-24 12:19:39
![](https://img.aspxhome.com/file/2023/3/55723_0s.png)
python之Socket网络编程详解
2021-05-29 14:43:22
![](https://img.aspxhome.com/file/2023/5/66365_0s.jpg)
使用Python获取并处理IP的类型及格式方法
2023-12-20 00:18:23
5行Python代码实现电脑永不息屏
2023-04-04 01:26:37
![](https://img.aspxhome.com/file/2023/5/105885_0s.png)
Pytorch:Conv2d卷积前后尺寸详解
2021-01-18 23:36:54
![](https://img.aspxhome.com/file/2023/0/118560_0s.png)
Python安装与卸载流程详细步骤(图解)
2023-10-05 19:36:21
![](https://img.aspxhome.com/file/2023/8/75148_0s.jpg)
网页设计配色基础:RGB与HSB
2008-05-06 12:23:00
![](https://img.aspxhome.com/file/UploadPic/20085/6/200856122520738s.jpg)
常用ASP自定义函数集 (S.Sams)
2010-05-27 12:24:00
Python利用zhdate模块实现农历日期处理
2023-03-07 22:10:20
网址导航的组织方法
2008-09-27 12:35:00
解决MySQL启动时1067错误
2010-09-30 14:09:00
使用Python制作自动推送微信消息提醒的备忘录功能
2023-02-26 18:19:40
![](https://img.aspxhome.com/file/2023/3/112393_0s.jpg)