标签:def name scale 通过 idt 技术分享 像素 view write
有趣,有趣,看到朋友的那个佩奇的图片,我就想了下,还是弄一张皮卡丘的
图片如下:
图片名称为ccc.jpg 代码里面可以改的
代码如下:
# -- coding: utf-8 -- from PIL import Image import argparse WIDTH = 90 HEIGHT = 45 ascii_char = list("@#&$*ox!i;.") def get_char(r, g, b, alpha=256): if alpha == 0: return ‘ ‘ length = len(ascii_char) gray = int(0.299 * r + 0.578 * g + 0.114 * b) unit = (256.0 + 1) / length return ascii_char[int(gray / unit)] if __name__ == ‘__main__‘: im = Image.open(‘ccc.jpg‘) scale = max(im.size[0] / WIDTH, im.size[1] / HEIGHT) WIDTH = im.size[0] / scale * 2 HEIGHT = im.size[1] / scale 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))) # 读取(j,i)像素点的r,g,b,alpha值用于计算灰度 txt += ‘\n‘ # 打印完一行后换行 # 字符画输出到文件 with open("output.txt", ‘w‘) as f: f.write(txt)
结果
标签:def name scale 通过 idt 技术分享 像素 view write
原文地址:https://www.cnblogs.com/liang2580/p/9147772.html