博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Python] IMG to Char
阅读量:7085 次
发布时间:2019-06-28

本文共 1811 字,大约阅读时间需要 6 分钟。

Change image into character

from PIL import Imageimport argparse#输入#命令行输入参数处理parser = argparse.ArgumentParser()parser.add_argument('file') #position argumentparser.add_argument('-o','--output') #optional argument and -o is a short optionparser.add_argument('--width', type = int, default = 80) #optional argumentparser.add_argument('--height', type = int, default = 80) #获取参数args = parser.parse_args()IMG = args.fileWIDTH = args.widthHEIGHT = args.heightOUTPUT = args.outputascii_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 = (256.0+1)/length    return ascii_char[int(gray/unit)]#输出if __name__ == '__main__':#When the Python interpreter reads a source file, it executes all of the code found in it.#in main program, __name__ = __main__    im = Image.open(IMG)    im = im.resize((WIDTH,HEIGHT), Image.NEAREST)#Image.resize(size, resample=0)#Parameters:    #size – The requested size in pixels, as a 2-tuple: (width, height).#resample – An optional resampling filter. This can be one of PIL.Image.NEAREST (use nearest neighbour), PIL.Image.BILINEAR (linear interpolation), PIL.Image.BICUBIC (cubic spline interpolation), or PIL.Image.LANCZOS (a high-quality downsampling filter). If omitted, or if the image has mode “1” or “P”, it is set PIL.Image.NEAREST.    txt = ""    for i in range(HEIGHT):        for j in range(WIDTH):            txt += get_char(*im.getpixel((j,i)))        txt += '\n'    print txt    if OUTPUT:        with open(OUTPUT, 'w') as f:            f.write(txt)    else:        with open("output.txt",'w') as f:            f.write(txt)

 

转载于:https://www.cnblogs.com/KennyRom/p/6354127.html

你可能感兴趣的文章
测试用例编写规范
查看>>
Linux vsftpd服务器
查看>>
使用ADRCI打oracle故障包
查看>>
简易更改win系统的连接
查看>>
Apache+PHP+SSL 安装步骤
查看>>
IPSEC ***学习笔记 (二)
查看>>
JQuery学习笔记-操作DOM元素
查看>>
【远程医疗专题】远程医疗论文30篇及解决方案10篇
查看>>
crondtab 定时任务
查看>>
【ios】导航跳转
查看>>
linux下建立回收站防止误删除及定期清空
查看>>
动态安全模型
查看>>
liuux/ Unix 文件管理命令(五)
查看>>
Hadoop学习笔记之二:HDFS体系架构
查看>>
foreman架构的引入4-安装Foreman1.6.3架构(foreman与puppetmaster分离)
查看>>
W和L
查看>>
TCP/IP协议解析
查看>>
德国罗森伯格推出MU Uniboot数据中心高密度布线系统
查看>>
Centos以及windows2003server系统下的密码重置
查看>>
CCNA学习笔记 一 基础方面
查看>>