-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg2txt.py
97 lines (87 loc) · 2.31 KB
/
img2txt.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Translate image to ASCII"""
from PIL import Image
from sys import argv, stderr
def round_color(color: int):
if not 0 <= color < 256:
raise ValueError("Color value must be between 0 and 255")
if color < 63:
return 0
elif color < 190:
return 127
else:
return 255
def pixel_to_str(pixel: tuple[int, int, int, int]):
rounded_pixel = tuple(map(round_color, pixel))
match rounded_pixel:
case 0, 0, 0, _:
return ' '
case 0, 0, 127, _:
return '1'
case 0, 0, 255, _:
return ')'
case 0, 127, 0, _:
return '!'
case 0, 127, 127, _:
return '3'
case 0, 127, 255, _:
return '>'
case 0, 255, 0, _:
return '|'
case 0, 255, 127, _:
return 'x'
case 0, 255, 255, _:
return '\\'
case 127, 0, 0, _:
return '@'
case 127, 0, 127, _:
return '5'
case 127, 0, 255, _:
return '$'
case 127, 127, 0, _:
return '['
case 127, 127, 127, _:
return '\u2593'
case 127, 127, 255, _:
return '='
case 127, 255, 0, _:
return '#'
case 127, 255, 127, _:
return 'L'
case 127, 255, 255, _:
return 'J'
case 255, 0, 0, _:
return '('
case 255, 0, 127, _:
return 'p'
case 255, 0, 255, _:
return '&'
case 255, 127, 0, _:
return '-'
case 255, 127, 127, _:
return 'i'
case 255, 127, 255, _:
return 'I'
case 255, 255, 0, _:
return '/'
case 255, 255, 127, _:
return 'X'
case 255, 255, 255, _:
return '@'
case err:
print(f"What is {err}?")
raise ValueError
def main(filename):
try:
img = Image.open(filename)
except (FileNotFoundError, IndexError):
print("Invalid file.", file=stderr)
exit()
pixels = img.load()
text: str = ""
for x in range(0, img.size[1],1):
for y in range(0, img.size[0] ,1):
text += pixel_to_str(pixels[y,x])
text += "\n"
print(text)
if __name__ == '__main__':
main(argv[1])