-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextbasedanimation.py
More file actions
131 lines (97 loc) · 3.02 KB
/
Copy pathtextbasedanimation.py
File metadata and controls
131 lines (97 loc) · 3.02 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from PIL import Image
from time import sleep
from os import system, name
# Pretty sure there are optimisations to be made, but eh, whatever
def getFrames(gifloc):
frames = []
frame = Image.open(gifloc)
i = 0
while True:
frames.append(frame.copy())
i += 1
try:
frame.seek(i)
except EOFError:
return frames
print("Welcome to the animation-to-text tool!")
loc = input("Enter file location: ")
img = Image.open(loc)
# frames = getFrames(loc)
scale = int(input("Enter how scaled down you want it to be (I suggest 4 for a 400 x 400px image): "))
frameRate = int(input("Frame rate (fps): "))
loopcount = int(input("How many times do you want to loop the animation? "))
# define our clear function
def clear():
# for windows
if name == 'nt':
_ = system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = system('clear')
def surfaceToText(surf, squish):
width, height = surf.size
# create empty grid
grid = []
for y in range(0, height, squish):
grid.append([])
for x in range(0, width, squish):
grid[y // squish].append(' ')
# do conversion
rgb_surf = surf.convert("RGB")
colortable = [ # x=r y=g z=b coordinates represented by where the color values are in the range;
# 0-85: 0, 86-170: 1, 171-255: 2
[
['&', '>', '+'],
['T', ';', 'O'],
['/', '?', 'o'],
],
[
['#', 'a', 'n'],
['`', '|', '*'],
['^', '\'', '-'],
],
[
['@', '=', 'e'],
['(', ')', '}'],
['"', '`', ' '],
],
]
for y in range(0, height, squish):
for x in range(0, width, squish):
# get color value
# col = surf.get_at((x, y))
col = rgb_surf.getpixel((x, y))
# col_val = col[0] + col[1] + col[2]
# assign char based on vale (might want to tinker around with this later)
char = colortable[col[2] // 85 - (1 if col[2] == 255 else 0)][col[1] // 85 - (1 if col[1] == 255 else 0)][col[0] // 85 - (1 if col[0] == 255 else 0)]
'''
if col_val <= 100:
char = "&"
elif 100 <= col_val <= 600:
char = '*'
else:
char = ' '
'''
grid[y // squish][x // squish] = char
return grid
def getTextedFrame(g):
s = ''
for row in g:
for c in row:
s += c * 2
s += '\n'
return s
print("Getting frames...")
frames = getFrames(loc)
print("Converting frames to text...")
textedFrames = []
for frame in frames:
textedFrames.append(surfaceToText(frame, scale))
print("Getting display frames...")
displayFrames = []
for i in range(len(textedFrames)):
displayFrames.append(getTextedFrame(textedFrames[i]))
for i in range(len(displayFrames) * loopcount):
clear()
print(displayFrames[i % len(displayFrames)])
sleep(1 / frameRate)