Change image into character

from PIL import Image
import argparse #输入
#命令行输入参数处理
parser = argparse.ArgumentParser() parser.add_argument('file') #position argument
parser.add_argument('-o','--output') #optional argument and -o is a short option
parser.add_argument('--width', type = int, default = 80) #optional argument
parser.add_argument('--height', type = int, default = 80) #获取参数
args = parser.parse_args() IMG = args.file
WIDTH = args.width
HEIGHT = args.height
OUTPUT = args.output 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 = (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)

[Python] IMG to Char的更多相关文章

  1. Python 学习手册, char 14 - 15

    Char 14 迭代器和解析器 可迭代的 : 支持iter的一个对象 迭代器  : iter 所返回的一个支持next(I)的对象 Python迭代工具会自动调用这些函数,我们也可以手动地应用迭代协议 ...

  2. Windows API Hooking in Python

    catalogue . 相关基础知识 . Deviare API Hook Overview . 使用ctypes调用Windows API . pydbg . winappdbg . dll inj ...

  3. 【新手学Python】一、基础篇

    由于以前处理数据用Matlab和C,最近要处理大量文本文件,用C写实在是太繁琐,鉴于Python的强大文本处理能力,以及其在Deep Learning上有着很大优势,本人打算从即日起学习Python, ...

  4. python 进程间共享数据 (一)

    def worker(num, mystr, arr): num.value *= 2 mystr.value = "ok" for i in range(len(arr)): a ...

  5. Python基础4- 字符串

    Python字符串是由数字.字母.下划线组成的一串字符,我们可以使用引号来创建字符串.如:str = 'Helloworld'在Python中没有char类型,单个字符也作为string使用; Pyt ...

  6. paip.判断字符是否中文与以及判读是否是汉字uapi python java php

    paip.判断字符是否中文与以及判读是否是汉字uapi python java php   ##判断中文的原理 注意: 中文与汉字CJKV 的区别..日本,韩国,新加坡,古越南等国家也用汉字,但不是中 ...

  7. paip.截取字符串byLastDot方法总结uapi python java php c# 总结

    paip.截取字符串byLastDot方法总结uapi python java php c# 总结 ========uapi   left_byLastDot   right_byLastDot 目前 ...

  8. OK - A byte of python - 读书笔记

    看这本书的目的:再熟悉基本概念. 大部分都是知道,但是需要 明确 出来的 概念. - 欢迎吐槽错误,非常感谢. <A byte of python> - THIS 1. 组织行 - 形式: ...

  9. Python调用C模块以及性能分析

    一.c,ctypes和python的数据类型的对应关系 ctypes type ctype Python type c_char char 1-character string c_wchar wch ...

随机推荐

  1. IOS开发 GCD介绍: 基本概念和Dispatch Queue

    iOS的三种多线程技术 1.NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 2.以下两点是苹果专门开发的“并发”技术,使得程序员可以不再去关心线程的具体使用问题 ØN ...

  2. CentOS 6.5 部署 Horizon

    以root用户进行部署,python源也可以使用 http://mirrors.aliyun.com/pypi/simple/ 修改系统 更改SElinux的配置文件 /etc/selinux/con ...

  3. [转载]Unicode中对中文字符的编码

    以前写过一篇贴子是写中文在unicode中的编码范围 unicode中文范围,但写的不是很详细,今天再次研究了下unicode,并给出详细的unicode取值范围. 本次研究的unicode对象是un ...

  4. ios PageControl and UIScrollView

    // //  AlbumViewController.m //  HwangKop08.18 // //  Created by rimi on 15/8/20. //  Copyright (c) ...

  5. [译]TLS中的RC4被攻破了,现在该怎么办?

    原文链接:https://community.qualys.com/blogs/securitylabs/2013/03/19/rc4-in-tls-is-broken-now-what 原文发表时间 ...

  6. linux自学(五)之开始centos学习,Xshell远程连接

    上一篇:linux自学(四)之开始centos学习,网络配置 前面操作都是在电脑中的虚拟机上操作的,比较麻烦,需要来回切换.下面我将使用远程连接工具Xshell进行操作,Xshell直接百度下载即可. ...

  7. js 时间date格式化

    js中没有类似java中的DateFormat类来处理日期格式化,可以自己写一个: var newDate=new Date(); var year=newDate.getFullYear(); va ...

  8. 关于上架AppStore最后一步的“出口合规信息”、“内容版权”、“广告标识符”的选择(转)

    这里只当作记录:http://blog.csdn.net/ashimar_a/article/details/51745675

  9. hadoop之 解析HDFS的写文件流程

    文件是如何写入HDFS的 ? 下面我们来先看看下面的“写”流程图:  假如我们有一个文件test.txt,想要把它放到Hadoop上,执行如下命令: 引用         # hadoop fs  - ...

  10. centos6 查看SELinux状态 关闭SELinux

    SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.在这种访问控制体系的限制下,进程只能访问那 ...