-
Notifications
You must be signed in to change notification settings - Fork 24
/
pic2chars.py
43 lines (35 loc) · 1.33 KB
/
pic2chars.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
# -*- coding: utf-8 -*-
import argparse
from PIL import Image
parser = argparse.ArgumentParser()
parser.add_argument('file', help='the picture to convert')
parser.add_argument('-o', '--output', help="the filename to output, default 'output.txt' ")
parser.add_argument('-w', '--width', type=int, help='the width of output file')
parser.add_argument('-ht', '--height', type=int, help='the height of output file')
args = parser.parse_args()
# You can add another str to change chars(left to right, black to white):
str1 = "@@WW##$$XXoo**""==::''..-- "
str2 = "01. "
chars = list(str2)
def get_char(r, b, g, alpha = 256):
if alpha == 0:
return ' '
gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
return chars[int(gray / 256.0 * len(chars))]
if __name__ == '__main__':
im = Image.open(args.file)
w = args.width if args.width else im.size[0]
h = args.height if args.height else im.size[1]
im = im.resize((w,h), Image.ANTIALIAS)
im = im.convert('RGBA')
txt = ""
for j in range(im.size[1]):
for i in range(im.size[0]):
txt += get_char(*im.getpixel((i,j)))
txt += '\n'
if args.output:
with open(args.output,'w') as f:
f.write(txt)
else:
with open("output.txt",'w') as f:
f.write(txt)