-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathascii.py
More file actions
51 lines (34 loc) · 1.2 KB
/
Copy pathascii.py
File metadata and controls
51 lines (34 loc) · 1.2 KB
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
#!/usr/bin/env python3
#coding: utf-8
from PIL import Image
import argparse
def get_char(r, g, b, alpha=256):
if alpha == 0:
return " "
ascii_chart = list("qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm ")
grey = (2126 * r + 7152 * g + 722 * b) / 10000
# assign grey number to char in ascii_chart
char_index = int((grey / float(alpha)) * len(ascii_chart))
return ascii_chart[char_index]
def write_file(out_file, text):
with open(out_file, 'w') as f:
f.write(text)
def main(in_file, width, height, out_file):
im = Image.open(in_file)
im = im.resize((width, height), Image.NEAREST)
text = ""
for j in xrange(height):
for i in xrange(width):
rgb_info = im.getpixel((i, j))
text += get_char(*rgb_info)
text += "\n"
print(text)
write_file(out_file, text)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("in_file", help="Name of Input File")
parser.add_argument("out_file", help="Name of Output File")
parser.add_argument("--width", type=int, default=50)
parser.add_argument("--height", type=int, default=50)
args = parser.parse_args()
main(args.in_file, args.width, args.height, args.out_file)