Python 实现简单的图片转字符画
2022-02-10 16:22
图片格式为jpg,将图片放入脚本同一文件夹下执行
from PIL import Image
name = input('请输入图片名称:')
img_weizhi = './' + name
img = Image.open(img_weizhi) # 打开
# img = Image.open("./img/cat.jpg") # 打开
#转换为黑白图片,参数"L"表示黑白模式
out = img.convert("L") # 灰度 先将彩色图片转换为黑白图片
width, height = out.size # 赋值
out = out.resize((int(width * 0.2),int(height * 0.1))) # 改变图像的大小
width, height = out.size # 赋值
#生成字符画所需的字符集
asciis = "@%#*+=-. "
texts = ""
for row in range(height):
for col in range(width):
gray = out.getpixel((col,row))
texts += asciis[int(gray / 255 * 8)]
texts += "\n"
with open( name + ".txt","w") as file:
file.write(texts)
本文来自博客园,作者:Haibara-Z3r0,转载请注明原文链接:https://www.cnblogs.com/Yu-0/articles/16337697.html