# coding=utf-8
from PIL import Image
# 打开图片
img = Image.open('C:/Users/57714/Desktop/src.jpg')
# 转换灰度
out = img.convert("L")
width, height = out.size
# 调整图片大小
out1 = out.resize((int(width*0.5),int(height*0.2)))
wid, hei = out1.size
asciis = '█▇▆▅▄▃▂▁ '
texts = ''
# 遍历像素点并置换
for ral in range(hei):
for caw in range(wid):
gray = out1.getpixel((caw, ral))
texts += asciis[int(gray / 255 * 8)]
texts += '\n'
# 保存为文本文件
with open ('C:/Users/57714/Desktop/text.txt','w',encoding = 'Utf-8') as fill:
fill.write(texts)
原图
转换后