首先要安装图像处理标准库,win+R打开运行窗口,输入cmd,在命令行中输入pip install pillow安装完成即可。
from PIL import Image
zoom = 0.5 #图片比例调整
vscale = 0.75 #图片长宽比例调整(中文字符大概长宽比为4:3)
img = Image.open("image.png") #打开待转化图像
out = img.convert("L")
out.show()
width,height = out.size
out = out.resize((int(width * zoom),int(height * zoom * vscale)))
width,height = out.size
asciis = "繁翻里图的花片个七二一" #可自行更改替换的文字,图片转化效果不理想时调整,尽量拉开“对比度”
text = ""
for row in range(height):
for col in range(width):
gray = out.getpixel((col,row))
text += asciis[int(gray / 255 * 10)]
text += "\n"
with open("文件地址/文件名.txt","w") as file: #选择保存txt文件的路径
file.write(text)
转换完成,是字符型蓝胖子了。