-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathasciinema2gif.py
30 lines (26 loc) · 967 Bytes
/
asciinema2gif.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
import json
import os.path
import subprocess
import sys
import tempfile
from pyte import Screen, Stream
from PIL import Image, ImageDraw, ImageFont
def main(path):
font = ImageFont.truetype("SourceCodePro-Regular.otf", size=16)
with tempfile.TemporaryDirectory() as dirname, open(path, 'r') as f:
j = json.load(f)
screen = Screen(j['width'], j['height'])
stream = Stream()
stream.attach(screen)
i = 0
for delay, text in j['stdout']:
img = Image.new("RGBA", (1280, 960))
draw = ImageDraw.Draw(img)
stream.feed(text)
draw.multiline_text((0, 0), "\n".join(screen.display), font=font)
img.save(os.path.join(dirname, '%04d.gif' % i))
i = i + 1
subprocess.run('gifsicle -d 10 --loopcount -O2 --output %s.gif %s/*.gif' % (path, dirname),
shell=True, check=True)
if __name__ == "__main__":
main(sys.argv[1])