美文网首页
python 命令行用字符显示图片

python 命令行用字符显示图片

作者: proud2008 | 来源:发表于2020-05-21 17:35 被阅读0次

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import base64
import datetime
import json
import os
import requests;
from PIL import Image

# 保证在微信客户端可调的通
headers = {'Content-Type': 'application/json',
           "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/4.0.1295.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2875.116 Safari/537.36 NetType/WIFI MicroMessenger/7.0.5 WindowsWechat"}

cookie = None;



def get_char(r, g, b, alpha=256):
    """
    我们定义的不重复的字符列表,灰度值小(暗)的用列表开头的符号,灰度值大(亮)的用列表末尾的符号
    # 将256灰度映射到70个字符上
    :param r:
    :param g:
    :param b:
    :param alpha:
    :return:
    """
    ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
    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)]


def getImg():
    """
    读验证码
    :return:
    """
    _imgUrl = "https://csdnimg.cn/cdn/content-toolbar/csdn-logo_.png?v=20190924.1"
    from io import BytesIO
    response = requests.get(_imgUrl)
    im = Image.open(BytesIO(response.content))
    # 保存到文件
    # html = requests.get(_imgUrl, headers=headers);
   #  _imgName = 's2.jpg'
   #  with open(_imgName, 'wb') as file:
    #     file.write(html.content)
    # im = Image.open(_imgName)
    # 尺寸调整  小些
    # im = im.resize((80, 40), Image.NEAREST)
    # os.remove(_imgName) #删除本地存储的文件
    txt = ""
    for i in range(im.height):
        for j in range(im.width):
            txt += get_char(*im.getpixel((j, i)))  # *解域将元组转成多个值
        txt += '\n'
    print(txt)
    # 新窗口显示图片
    # plt.figure(figsize=(3, 2))
    # plt.imshow(im)
    # plt.show()
    return



getImg()



#! /usr/bin/env python
# -*- coding: utf-8 -*-
# ascii.py 来自网络
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 = 80) #输出字符画宽
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;:,\"^`'. ")

# 将256灰度映射到70个字符上
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 = ""

    #将图片看成由像素点组成的二维数组,i代表每一行,j代表每一列
    for i in range(HEIGHT):
        for j in range(WIDTH):
            #getpixel()函数的参数是由每个像素点在图片中的相对位置(w,h)组成的元组
            #返回值是一个代表图片像素值的(r,g,b,alpha)元组
            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 命令行用字符显示图片

      本文链接:https://www.haomeiwen.com/subject/etuvrhtx.html