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. Recorder︱深度学习小数据集表现、优化(Active Learning)、标注集网络获取

    一.深度学习在小数据集的表现 深度学习在小数据集情况下获得好效果,可以从两个角度去解决: 1.降低偏差,图像平移等操作 2.降低方差,dropout.随机梯度下降 先来看看深度学习在小数据集上表现的具 ...

  2. linux下awk内置函数的使用(split/substr/length)

    一.split 初始化和类型强制  awk的内建函数split允许你把一个字符串分隔为单词并存储在数组中.你可以自己定义域分隔符或者使用现在FS(域分隔符)的值.格式:    split (strin ...

  3. OC中使用单例模式

    static Config * instance = nil; +(Config *) Instance { @synchronized(self) { if(nil == instance) { [ ...

  4. 在crontab中执行shell脚本的问题

    crontab中记录的编写比较简单,下面是一个示例: * * * /app/tpssapp/ftpsrc/tools/statTables/statTables.sh > /app/tpssap ...

  5. BZOJ1597土地购买 【斜率优化DP】

    BZOJ1597土地购买 [斜率优化DP] Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足( ...

  6. 重温CLR(十二) 委托

    回调函数是一种非常有用的编程机制,它的存在已经有很多年了..NET通过委托来提供回调函数机制.不同于其他平台(比如非托管C++)的回调机制,委托的功能要多得多.例如,委托确保回调方法是类型安全的(这是 ...

  7. 【spring源码学习】spring集成orm数据框架

    [一]简易的数据源配置 (1)配置文件 <!--springJdbcTemplemate数据操作配置信息 --> <bean id="driver" class= ...

  8. 【最全 干货 实例】 缓存手册(Memcached、redis、RabbitMQ)

    http://www.cnblogs.com/suoning/p/5807247.html 本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 re ...

  9. drone 学习二 pipeline 说明

    1. 基本语法 pipeline: backend: image: golang commands: - go build - go test frontend: image: node comman ...

  10. c#代码加密

    源代码保护:怎样利用MaxtoCode加密dotNet源代码 http://www.webkaka.com/blog/archives/MaxtoCode-encrypt-dotnet-program ...