此python文件来自D7哥, 放在这里备份.

  • 用法
python3 PIL\&argparse.py  1.jpg -o test.txt --width 300 --height 300

python3 	xxx.py		要识别的img	输出到文件	宽度		高度
  • 程序
# coding:utf-8
from PIL import Image
import argparse #命令行输入参数处理
parser = argparse.ArgumentParser() parser.add_argument('file') #输入文件
parser.add_argument('-o', '--output') #输出文件
parser.add_argument('--width', type = int, default = 60) #输出字符画宽
parser.add_argument('--height', type = int, default = 10) #输出字符画高 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__': im = Image.open(IMG)
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)))
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识别图片生成字符模式的更多相关文章

  1. 通过python将图片生成字符画

    基础知识: 1.python基础知识   快速学习链接:https://www.shiyanlou.com/courses/214 2.linux命令行操作   快速学习链接:https://www. ...

  2. python实战===图片转换为字符的源码(转)

    #cmd执行命令为>>> python xx.py pic.png#-*- coding:utf-8 -*- from PIL import Image import argpars ...

  3. python 识别图片验证码报IOError

    说一下困扰了我一周的问题:识别图片验证码 本来我按照安装步骤(http://www.cnblogs.com/yeayee/p/4955506.html?utm_source=tuicool&u ...

  4. python识别图片文字

    因为学校要求要刷一门叫<包装世界>的网课,而课程里有200多道选择题,而且只能在手机完成,网页版无法做题,而看视频是不可能看视频的,这辈子都不可能看...所以写了几行代码来进行百度搜答案. ...

  5. python 识别图片上的数字

    https://blog.csdn.net/qq_31446377/article/details/81708006 ython 3.6 版本 Pytesseract 图像验证码识别 环境: (1) ...

  6. OpenCV+Python识别车牌和字符分割的实现

    本篇文章主要基于python语言和OpenCV库(cv2)进行车牌区域识别和字符分割,开篇之前针对在python中安装opencv的环境这里不做介绍,可以自行安装配置! 车牌号检测需要大致分为四个部分 ...

  7. python爬虫20 | 小帅b教你如何使用python识别图片验证码

    当你在爬取某些网站的时候 对于你的一些频繁请求 对方会阻碍你 常见的方式就是使用验证码 验证码的主要功能 就是区分你是人还是鬼(机器人) 人 想法设法的搞一些手段来对付技术 而 技术又能对付人们的想法 ...

  8. python 识别图片文字

    今天群里有兄弟问如何把图片的文字给识别出来 对于python来说这不是小菜一碟吗,于是乎让pupilheart狠狠的吹了一波(哈哈,竟然没懂),下面将整个实现过程给大家实现下: 方法一:自己搞定ORC ...

  9. python验证码图片生成

    环境:win10(64位)+pycharm2018+pillow5.4+python3.7 对Django的跨站请求保护的有所了解的同学会知道{%csrf_token%}在实际上作用并不是那么大,只要 ...

随机推荐

  1. 实验一Java开发环境的熟悉-3

    实现学生成绩管理功能(增删改,排序,查找),并进行测试(正常情况,异常情况,边界情况). 代码:

  2. Serveral effective linux commands

    1. 统计当前文件夹下文件个数(不包括子目录下文件): $ ls -l | grep "^-" | wc -l 2. 统计当前文件夹下文件个数(包括子目录下文件): $ ls -l ...

  3. iOS UI基础-18.0 UIView

    设置边框 UIView *bgView = [[UIView alloc] init]; bgView.backgroundColor = [UIColor whiteColor]; self.bgV ...

  4. Bootstrap-按钮相关的class

    .btn          基础class.btn-default  白底黑字的按钮.btn-warning  红色按钮.btn-success  绿色按钮.btn-info     浅蓝色按钮.bt ...

  5. c#Lock学习笔记

    转自 http://www.cnblogs.com/tianma3798/p/6290158.html 参考官网  https://docs.microsoft.com/zh-cn/dotnet/cs ...

  6. Vue系列之 => 动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. html5-超级链接

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  8. 准备mysql-connector-java

    下载mysql-connector-java:https://mvnrepository.com/artifact/mysql/mysql-connector-java 导入mysql-connect ...

  9. urllib2 post请求方式,带cookie,添加请求头

    #encoding = utf-8 import urllib2import urllib url = 'http://httpbin.org/post'data={"name": ...

  10. python模板字符串和格式化字符串

    模板字符串:用string模块里的Template Template()里面把字符串中某个值用设置变量${key}的方式先写好,然后在substitute()的方式把变量用其他值代替,就完成了字符串的 ...