python 图片转字符画

from PIL import Image
IMG = '1.jpg' #设置图片文件
WIDTH = 300 #设置字符画的宽
HEIGHT =400 #设置字符画的高
OUTPUT = 'output5.txt'  #设置存放字符画的文本文件
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")    #设置显示的字符集
def get_char(r,g,b,alpha = 256):
    if alpha == 0:
        return ' '
    length = len(ascii_char)
    gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
    unit = (255.0 + 1)/length
    return ascii_char[int(gray/unit)]
if __name__ == '__main__':
    im = Image.open(IMG)
    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'
    print(txt)
    with open(OUTPUT,'w') as f:
        f.write(txt)

在这里插入图片描述

posted @ 2022-06-10 11:22  CJK'sBLOG  阅读(25)  评论(0编辑  收藏  举报