-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·85 lines (59 loc) · 1.82 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import argparse
import curses
from PIL import Image
from drawille import Canvas
def pad(s, w):
if len(s) < w:
s += (" " * (w - len(s) - 1))
return s
def main(args, opts):
curses.use_default_colors()
curses.curs_set(0)
win = curses.initscr()
source = Image.open(opts.filename)
c = Canvas()
try:
while True:
(wh, ww) = win.getmaxyx()
try:
source.seek(source.tell() + 1)
except:
if opts.onetime:
break
source.seek(0)
img = (source
.resize(((ww - 1) * 2, wh * 4))
.convert("1")
)
w = img.width
(x, y) = (0, 0)
for v in img.getdata():
if opts.reverse:
if not v:
c.set(x, y)
else:
if v:
c.set(x, y)
x += 1
if w <= x:
x = 0
y += 1
for r in range(wh):
line = c.rows(min_y=(r*4), max_y=((r+1)*4))[0]
win.addnstr(r, 0, pad(line, ww), ww)
win.refresh()
c.clear()
time.sleep(opts.interval)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Terminal Animation")
parser.add_argument("filename", help="Source filename (anime-gif)")
parser.add_argument("-n", "--interval", type=float, default=0.1)
parser.add_argument("-r", "--reverse", action="store_true")
parser.add_argument("-1", "--onetime", action="store_true")
args = parser.parse_args()
curses.wrapper(main, args)