File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ Python技术 公众号文章代码库
1010
1111## 实例代码
1212
13+ [ 用 Python 实现图片转字符画,so easy!] ( https://github.com/JustDoPython/python-examples/tree/master/xianhuan/charPic ) :用 Python 实现图片转字符画,so easy!
14+
1315[ 绝了!自动点赞,我用 PyAutoGUI!] ( https://github.com/JustDoPython/python-examples/tree/master/xianhuan/pyautogui2 ) :绝了!自动点赞,我用 PyAutoGUI!
1416
1517[ 这个自动化利器,Pythoner都在用!] ( https://github.com/JustDoPython/python-examples/tree/master/xianhuan/pdffit ) :自动化利器,Pythoner都在用!
Original file line number Diff line number Diff line change 1+ #!/usr/bin/python3
2+ #coding: utf-8
3+
4+ from PIL import Image
5+
6+ ascii_char = list ('M3NB6Q#OC?7>!:–;. ' )
7+
8+ def get_char (r , g , b , alpha = 256 ):
9+ if alpha == 0 :
10+ return ' '
11+
12+ grey = (2126 * r + 7152 * g + 722 * b ) / 10000
13+
14+ char_idx = int ((grey / (alpha + 1.0 )) * len (ascii_char ))
15+ return ascii_char [char_idx ]
16+
17+ def write_file (out_file_name , content ):
18+ with open (out_file_name , 'w' ) as f :
19+ f .write (content )
20+
21+ def main (file_name = "input.jpg" , width = 100 , height = 80 , out_file_name = 'output.txt' ):
22+ text = ''
23+ im = Image .open (file_name )
24+ im = im .resize ((width , height ), Image .NEAREST )
25+ for i in range (height ):
26+ for j in range (width ):
27+ text += get_char (* im .getpixel ((j , i )))
28+ text += '\n '
29+ print (text )
30+ write_file (out_file_name , text )
31+
32+ if __name__ == '__main__' :
33+ main ('dance.png' )
34+
You can’t perform that action at this time.
0 commit comments