-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascii_art.py
35 lines (26 loc) · 1.23 KB
/
ascii_art.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
import pyglet, sys, os, time
def animgif_to_ASCII_animation(animated_gif_path):
# map greyscale to characters
chars = ('#', '#', '@', '%', '=', '+', '*', ':', '-', '.', ' ')
clear_console = 'clear' if os.name == 'posix' else 'CLS'
# load image
anim = pyglet.image.load_animation(animated_gif_path)
# Step through forever, frame by frame
while True:
for frame in anim.frames:
# Gets a list of luminance ('L') values of the current frame
data = frame.image.get_data('L', frame.image.width)
# Built up the string, by translating luminance values to characters
outstr = ''
for (i, pixel) in enumerate(data):
outstr += chars[(ord(pixel) * (len(chars) - 1)) / 255] + \
('\n' if (i + 1) % frame.image.width == 0 else '')
# Clear the console
os.system(clear_console)
# Write the current frame on stdout and sleep
sys.stdout.write(outstr)
sys.stdout.flush()
time.sleep(0.1)
# run the animation based on some animated gif
animgif_to_ASCII_animation(
u'D:\Web Pages\WordPress Pages\elementor\assets\images')