Skip to content

Commit 853f4ff

Browse files
committed
add code
1 parent 6139c66 commit 853f4ff

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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)

0 commit comments

Comments
 (0)