[Python] IMG to Char
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的更多相关文章
- Python 学习手册, char 14 - 15
Char 14 迭代器和解析器 可迭代的 : 支持iter的一个对象 迭代器 : iter 所返回的一个支持next(I)的对象 Python迭代工具会自动调用这些函数,我们也可以手动地应用迭代协议 ...
- Windows API Hooking in Python
catalogue . 相关基础知识 . Deviare API Hook Overview . 使用ctypes调用Windows API . pydbg . winappdbg . dll inj ...
- 【新手学Python】一、基础篇
由于以前处理数据用Matlab和C,最近要处理大量文本文件,用C写实在是太繁琐,鉴于Python的强大文本处理能力,以及其在Deep Learning上有着很大优势,本人打算从即日起学习Python, ...
- python 进程间共享数据 (一)
def worker(num, mystr, arr): num.value *= 2 mystr.value = "ok" for i in range(len(arr)): a ...
- Python基础4- 字符串
Python字符串是由数字.字母.下划线组成的一串字符,我们可以使用引号来创建字符串.如:str = 'Helloworld'在Python中没有char类型,单个字符也作为string使用; Pyt ...
- paip.判断字符是否中文与以及判读是否是汉字uapi python java php
paip.判断字符是否中文与以及判读是否是汉字uapi python java php ##判断中文的原理 注意: 中文与汉字CJKV 的区别..日本,韩国,新加坡,古越南等国家也用汉字,但不是中 ...
- paip.截取字符串byLastDot方法总结uapi python java php c# 总结
paip.截取字符串byLastDot方法总结uapi python java php c# 总结 ========uapi left_byLastDot right_byLastDot 目前 ...
- OK - A byte of python - 读书笔记
看这本书的目的:再熟悉基本概念. 大部分都是知道,但是需要 明确 出来的 概念. - 欢迎吐槽错误,非常感谢. <A byte of python> - THIS 1. 组织行 - 形式: ...
- Python调用C模块以及性能分析
一.c,ctypes和python的数据类型的对应关系 ctypes type ctype Python type c_char char 1-character string c_wchar wch ...
随机推荐
- linux 文件上传&软件安装(rpm)
文件的上传与下载(linux -linux ) 实例1:从远处复制文件到本地目录命令:scp root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz /o ...
- int('x', base)中的base参数
>>> int('12', 16) 16表示'12'就是16进制数,int()要将这个16进制数转化成10进制.
- CF1117C Magic Ship
CF1117C Magic Ship 考虑到答案具单调性(若第 \(i\) 天能到达目的点,第 \(i+1\) 天只需向风向相反的方向航行),可以二分答案. 现在要考虑给出一个天数 \(m\) ,问 ...
- python笔记-9(subprocess模块、面向对象、socket入门)
一.subprocess 模块 1.了解os.system()与os.popen的区别及不足 1.1 os.system()可以执行系统指令,将结果直接输出到屏幕,同时可以将指令是否执行成功的状态赋值 ...
- PHP 数组转json_encode,单个数组下标为了0时不对??
在 php 数组转json时,假如 有一个数组下标是顺序的,他json_encode后会直接变成一个简版二维json, $arr = ['1'=>1,'2'=>2]; echo (json ...
- 《DSP using MATLAB》示例Example 8.11
%% ------------------------------------------------------------------------ %% Output Info about thi ...
- 【DUBBO】 Dubbo原理解析-Dubbo内核实现之基于SPI思想Dubbo内核实现
转载:http://blog.csdn.net/quhongwei_zhanqiu/article/details/41577235 SPI接口定义 定义了@SPI注解 public @interfa ...
- gradle 项目构建以及发布maven 私服&& docker 私服构建发布
1. 项目结构 2. 代码说明 a. Dockerfile docker 构建的 FROM openjdk:8-jre-alpine WORKDIR /appdemo/ COPY build/di ...
- Terraform 多云管理工具
1. 介绍 Terraform is a tool for building, changing, and versioning infrastructure safely and efficie ...
- 使用模块化编译缩小 apk 体积
libcocos2dlua.so编译出来有11M多,其中包含了很多不需要的模块,模块化编译,把不需要用到的模块弄成0,体积就小了. 如: 修改D:\codeide\sDiShu2formm\frame ...