Skip to content

Commit cb012a9

Browse files
committed
Added Image to ASCII Converter
1 parent bad384c commit cb012a9

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

Image-To-ASCII/ascii_image.txt

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
2+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
3+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
4+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
6+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
7+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
8+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
9+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@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@@@#++++++++;;;;;;;;#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
20+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@############;:;;;;;;##SS#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
21+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@?+;;;;;;;;;;;;;;;;;;;;#+:::+#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
22+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*::::::::::::::;;;;;;;;#+::::;#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
23+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@?:::::::::::::;;;;;;;;;;#+:::::*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
24+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#;:::::::::::::;;;;;;;;;;#+:::::;#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
25+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@S::::::::::::::;;;;;;;;:+#;::::::S@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
26+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@?::::::::::::::;;;;;;;;:%S;::::::?@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
27+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*:::::::::::::::::::::;%@*:::::::*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
28+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*:::::::::+?%%%%%%%%%S#@?;;::::::*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
29+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*::::::::%@#%%%%%%????*+;;;::::::*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
30+
@@@@@@@@@@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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
51+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
52+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
53+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
54+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
55+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
56+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Image-To-ASCII/main.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from PIL import Image
2+
3+
# ascii characters used to build the output text
4+
ASCII_CHARS = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", "."]
5+
6+
# resize image according to a new width
7+
def resize_image(image, new_width=100):
8+
width, height = image.size
9+
ratio = height/width
10+
new_height = int(new_width * ratio)
11+
resized_image = image.resize((new_width, new_height))
12+
return(resized_image)
13+
14+
# convert each pixel to grayscale
15+
def grayify(image):
16+
grayscale_image = image.convert("L")
17+
return(grayscale_image)
18+
19+
# convert pixels to a string of ascii characters
20+
def pixels_to_ascii(image):
21+
pixels = image.getdata()
22+
characters = "".join([ASCII_CHARS[pixel//25] for pixel in pixels])
23+
return(characters)
24+
25+
def main(new_width=100):
26+
# attempt to open image from user-input
27+
path = input("Enter a valid pathname to an image:\n")
28+
try:
29+
image =Image.open(path)
30+
except:
31+
print(path, " is not a valid pathname to an image.")
32+
return
33+
34+
# convert image to ascii
35+
new_image_data = pixels_to_ascii(grayify(resize_image(image)))
36+
37+
# format
38+
pixel_count = len(new_image_data)
39+
ascii_image = "\n".join([new_image_data[index:(index+new_width)] for index in range(0, pixel_count, new_width)])
40+
41+
# print result
42+
print(ascii_image)
43+
44+
# save result to "ascii_image.txt"
45+
with open("Image-To-ASCII/ascii_image.txt", "w") as f:
46+
f.write(ascii_image)
47+
48+
# run program
49+
main()

Image-To-ASCII/python.jpg

213 KB
Loading

Image-To-ASCII/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pillow

0 commit comments

Comments
 (0)