-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascii-image.py
56 lines (46 loc) · 1.32 KB
/
ascii-image.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
#-*-coding:utf-8-*-
from PIL import Image
import sys
import os
class BColor:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def _main():
try:
pic = os.path.abspath(sys.argv[1])
except:
print('指定图片路径')
img = Image.open(pic)
width = int(img.size[0])
height = int(img.size[1])
gray_img = img.convert('L')
scale = width // 100
char_lst = '.:-=+*#%@'
char_len = len(char_lst)
arr = []
for y in range(0, height, scale):
for x in range(0, width,scale):
brightness = 0
r= g = b = 0
count = 0
for ix in range(scale):
for iy in range(scale):
if(x + ix) == width or (y + iy) == height:
break;
count += 1
b = 255 - gray_img.getpixel((x + ix, y + iy))
brightness += b
choice = int(char_len * (brightness // count / 255) ** 1.2)
if choice >= char_len:
choice = char_len
sys.stdout.write(char_lst[choice])
sys.stdout.write("\n")
sys.stdout.flush()
if __name__ == '__main__':
_main()