-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.py
66 lines (51 loc) · 1.88 KB
/
12.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import argparse
from PIL import Image
def handle_command():
'命令行参数处理'
parser = argparse.ArgumentParser()
parser.add_argument('filename',help = 'C:/Users/asus/Desktop/ascii_dora.png')
parser.add_argument('-o','--output',help = '是否输出文件')
parser.add_argument('--width',type = int,default = 80)
parser.add_argument('--heigth',type = int,default = 80)
#获取命令行参数
return parser.parse_args()
args = handle_command()
class Ptrancefrom(object):
'实现将图片转化为字符'
def __init__(self,img,width,heigth):
self.img = img
self.width = width
self.heigth = heigth
self.ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
def get_char(self,r,b,g,alpha = 256):
'将256范围的灰度值映射到70个字符上'
#灰度值为0时对应字符为空格
if alpha == 0:
return ' '
length = len(self.ascii_char)
#灰度值的计算公式
gray = int(0.2126 *r + 0.7152*g + 0.0722*b)
unit = (256.0 + 1)/length
return self.ascii_char[int(gray/unit)]
def print_picture(self):
'打印图形'
#打开图片
im = Image.open(self.img)
#设置图片像素的大小
im = im.resize((self.width,self.heigth),Image.NEAREST)
txt = ""
for i in range(self.heigth):
for j in range(self.width):
txt += self.get_char(*im.getpixel((j,i)))
txt += '\n'
print(txt)
def write_to_file(self):
'将生成的字符图片写入到文件'
if args.output:
with open(args.output,'w') as f:
f.write(txt)
else:
with open('output.txt','w') as f:
f.write(txt)
pic = Ptrancefrom(args.filename,args.width,args.heigth)
pic.print_picture()