File tree Expand file tree Collapse file tree 1 file changed +10
-5
lines changed Expand file tree Collapse file tree 1 file changed +10
-5
lines changed Original file line number Diff line number Diff line change 2
2
3
3
ASCII_CHARS = '@B%8WM#*oahkbdpwmZO0QCJYXzcvnxrjft/\|()1{}[]-_+~<>i!lI;:,"^`\' . '
4
4
5
+
5
6
def resize_image (image , new_width = 100 ):
6
7
width , height = image .size
7
8
ratio = height / width
8
9
new_height = int (new_width * ratio )
9
10
resized_image = image .resize ((new_width , new_height ))
10
11
return resized_image
11
12
13
+
12
14
def grayify (image ):
13
15
return image .convert ("L" )
14
16
17
+
15
18
def pixels_to_ascii (image ):
16
19
pixels = image .getdata ()
17
20
ascii_str = ""
18
21
for pixel in pixels :
19
22
ascii_str += ASCII_CHARS [pixel // 25 ]
20
23
return ascii_str
21
24
25
+
22
26
def main (image_path , new_width = 100 ):
23
27
try :
24
28
image = Image .open (image_path )
@@ -29,16 +33,17 @@ def main(image_path, new_width=100):
29
33
image = resize_image (image , new_width )
30
34
image = grayify (image )
31
35
ascii_str = pixels_to_ascii (image )
32
-
36
+
33
37
ascii_width = image .width
34
-
38
+
35
39
ascii_img = ""
36
40
for i in range (0 , len (ascii_str ), ascii_width ):
37
41
ascii_img += ascii_str [i :i + ascii_width ] + "\n "
38
-
42
+
39
43
print (ascii_img )
40
44
45
+
41
46
if __name__ == "__main__" :
42
- image_path = "pngwing.com (1).png"
43
- new_width = 100
47
+ image_path = "pngwing.com (1).png"
48
+ new_width = 100
44
49
main (image_path , new_width )
You can’t perform that action at this time.
0 commit comments