File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
doudou/2020-05-17-character-drawing Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ from PIL import Image
2+
3+ WIDTH , HEIGHT = 80 , 40
4+ ascii_char = list ("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\" ^`'. " )
5+
6+
7+ def get_char_by_rgb (r , g , b , alpha = 256 ):
8+ if alpha == 0 :
9+ return ' '
10+ length = len (ascii_char )
11+ gray = int (0.2126 * r + 0.7152 * g + 0.0722 * b )
12+ unit = 256.0 / length
13+ return ascii_char [int (gray / unit )]
14+
15+
16+ def process_image (image_path , file_path = 'out.txt' ):
17+ img = Image .open (image_path )
18+ img = img .resize ((WIDTH , HEIGHT ))
19+ width , height = img .size
20+ txt = ""
21+ for x in range (height ):
22+ for y in range (width ):
23+ txt += get_char_by_rgb (* img .getpixel ((y , x )))
24+ txt += '\n '
25+
26+ with open (file_path , 'w' ) as f :
27+ f .write (txt )
28+ print (txt )
29+
30+
31+ if __name__ == '__main__' :
32+ image_path , file_path = '/tmp/qq.jpg' , '/tmp/qq.txt'
33+ process_image (image_path , file_path )
You can’t perform that action at this time.
0 commit comments