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 22
33ASCII_CHARS = '@B%8WM#*oahkbdpwmZO0QCJYXzcvnxrjft/\|()1{}[]-_+~<>i!lI;:,"^`\' . '
44
5+
56def resize_image (image , new_width = 100 ):
67 width , height = image .size
78 ratio = height / width
89 new_height = int (new_width * ratio )
910 resized_image = image .resize ((new_width , new_height ))
1011 return resized_image
1112
13+
1214def grayify (image ):
1315 return image .convert ("L" )
1416
17+
1518def pixels_to_ascii (image ):
1619 pixels = image .getdata ()
1720 ascii_str = ""
1821 for pixel in pixels :
1922 ascii_str += ASCII_CHARS [pixel // 25 ]
2023 return ascii_str
2124
25+
2226def main (image_path , new_width = 100 ):
2327 try :
2428 image = Image .open (image_path )
@@ -29,16 +33,17 @@ def main(image_path, new_width=100):
2933 image = resize_image (image , new_width )
3034 image = grayify (image )
3135 ascii_str = pixels_to_ascii (image )
32-
36+
3337 ascii_width = image .width
34-
38+
3539 ascii_img = ""
3640 for i in range (0 , len (ascii_str ), ascii_width ):
3741 ascii_img += ascii_str [i :i + ascii_width ] + "\n "
38-
42+
3943 print (ascii_img )
4044
45+
4146if __name__ == "__main__" :
42- image_path = "pngwing.com (1).png"
43- new_width = 100
47+ image_path = "pngwing.com (1).png"
48+ new_width = 100
4449 main (image_path , new_width )
You can’t perform that action at this time.
0 commit comments