//目录

PIL 图像字符画绘制

from PIL import Image

ascii_char = list('"$%_&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-/+@<>i!;:,\^`.')

def get_char(r,g,b,alpha = 256):
    if alpha == 0:
        return ' '
    gray = int(0.2126*r+0.715*g+0.0722*b)
    unit = 256/len(ascii_char)
    return ascii_char[int(gray//unit)]


def main():
    file = input()
    im = Image.open(file)

    assert isinstance(im, Image.Image)

    W,H = 100,60
    im = im.resize((W,H))

    txt = ""
    fo = open("pic_char.txt","w")

    for i in range(H):
        txt=""
        for j in range(W):
            r,g,b = im.getpixel((j,i))
            txt += get_char(r,g,b)
        txt+='\n'
        fo.write(txt)
    fo.close()
main()

 

PIL第三方类库,无代码提示

assert isinstance(im, Image.Image)

posted @ 2018-10-22 17:12  小草的大树梦  阅读(963)  评论(0编辑  收藏  举报