Skip to content

Commit 0f969a6

Browse files
committed
提交代码
1 parent fa280f3 commit 0f969a6

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

xianhuan/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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都在用!

xianhuan/charPic/demo.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+

0 commit comments

Comments
 (0)