-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathintro.py
73 lines (59 loc) · 2.51 KB
/
intro.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
import PIL.Image
import art
from art import text2art
from console import console
# ascii characters used to build the output text
ASCII_CHARS = ["@", "#", "S", "%", "\"", "*", "+", ";", ":", ",", " "]
# resize image according to a new width
def resize_image(image, new_width=100):
width, height = image.size
# print(image.size)
ratio = height/width
new_height = int(new_width * ratio)-40
resized_image = image.resize((new_width, new_height ))
return(resized_image)
# convert each pixel to grayscale
def grayify(image):
grayscale_image = image.convert("L")
return(grayscale_image)
# convert pixels to a string of ascii characters
def pixels_to_ascii(image):
pixels = image.getdata()
characters = "".join([ASCII_CHARS[pixel//25] for pixel in pixels])
return(characters)
def main(new_width=100):
# attempt to open image from user-input
path = "./images/Wnormal1.jpg"
try:
image = PIL.Image.open(path)
except:
print(path, " is not a valid pathname to an image.")
return
# convert image to ascii
new_image_data = pixels_to_ascii(grayify(resize_image(image)))
# format
pixel_count = len(new_image_data)
ascii_image = "\n".join([new_image_data[index:(index+new_width)] for index in range(0, pixel_count, new_width)])
# print result
# print(ascii_image)
# save result to "ascii_image.txt"
with open("./images/ascii_image.txt", "w") as f:
f.write(ascii_image)
with open("./images/ascii_image.txt", "r") as f:
for line in f.readlines():
console(line,color="BLUE")
# run program
print()
main()
# my_art = text2art("successfully installed",font='bulbhead',chr_ignore=True) # Return ASCII text (default font)
# print(my_art)
console("""
__ _ _ _ _ _ _ _
/ _| | | | (_) | | | | | | |
___ _ _ ___ ___ ___ ___ ___| |_ _ _| | |_ _ _ _ __ ___| |_ __ _| | | ___ __| |
/ __| | | |/ __/ __/ _ \/ __/ __| _| | | | | | | | | | | '_ \/ __| __/ _` | | |/ _ \/ _` |
\__ \ |_| | (_| (_| __/\__ \__ \ | | |_| | | | |_| | | | | | \__ \ || (_| | | | __/ (_| |
|___/\__,_|\___\___\___||___/___/_| \__,_|_|_|\__, | |_|_| |_|___/\__\__,_|_|_|\___|\__,_|
__/ |
|___/
""",color='GREEN')