1 Star 1 Fork 0

姚佳瑞/Python-Spider

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
字符画.py 1.59 KB
Copy Edit Raw Blame History
Scar authored 2019-05-15 09:40 +08:00 . Add files via upload
# encoding: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 = 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;:,\"^`'. ")
ascii_char = list("01")
# 将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)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/Jruing/Python-Spider.git
git@gitee.com:Jruing/Python-Spider.git
Jruing
Python-Spider
Python-Spider
master

Search