Skip to content

Commit 94a3472

Browse files
Merge pull request #2866 from smty2018/ascii
Image to ASCII Art Converter
2 parents 0921ecb + e0b17de commit 94a3472

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

Image to ASCII Art/app.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from PIL import Image
2+
3+
ASCII_CHARS = '@B%8WM#*oahkbdpwmZO0QCJYXzcvnxrjft/\|()1{}[]-_+~<>i!lI;:,"^`\'. '
4+
5+
def resize_image(image, new_width=100):
6+
width, height = image.size
7+
ratio = height / width
8+
new_height = int(new_width * ratio)
9+
resized_image = image.resize((new_width, new_height))
10+
return resized_image
11+
12+
def grayify(image):
13+
return image.convert("L")
14+
15+
def pixels_to_ascii(image):
16+
pixels = image.getdata()
17+
ascii_str = ""
18+
for pixel in pixels:
19+
ascii_str += ASCII_CHARS[pixel // 25]
20+
return ascii_str
21+
22+
def main(image_path, new_width=100):
23+
try:
24+
image = Image.open(image_path)
25+
except Exception as e:
26+
print(e)
27+
return
28+
29+
image = resize_image(image, new_width)
30+
image = grayify(image)
31+
ascii_str = pixels_to_ascii(image)
32+
33+
ascii_width = image.width
34+
35+
ascii_img = ""
36+
for i in range(0, len(ascii_str), ascii_width):
37+
ascii_img += ascii_str[i:i+ascii_width] + "\n"
38+
39+
print(ascii_img)
40+
41+
if __name__ == "__main__":
42+
image_path = "pngwing.com (1).png"
43+
new_width = 100
44+
main(image_path, new_width)
547 KB
Loading

Image to ASCII Art/readme.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Image to ASCII Art Converter
2+
3+
This is a Python script that converts an image into ASCII art. It takes an image as input, resizes it, converts it to grayscale, and then generates an ASCII art representation of the image using a predefined set of ASCII characters.
4+
5+
## Requirements
6+
7+
- Python 3.x
8+
- Pillow (PIL Fork) library

0 commit comments

Comments
 (0)