-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
62 lines (49 loc) · 1.62 KB
/
compile.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
from PIL import Image, ImageDraw
import json
import sys
import os
# from https://stackoverflow.com/questions/15682537
def to_ansi(pixel):
r, g, b = pixel
if r == g and g == b:
if r < 8:
return 16
if r > 248:
return 231
return round(((r - 8) / 247) * 24) + 232
ansi = (16
+ (36 * round(r / 255 * 5))
+ (6 * round(g / 255 * 5))
+ round(b / 255 * 5));
return ansi
def compile_images(directory, limit, width, height):
prev_image = None
for idx, file in enumerate(sorted(os.listdir(directory))):
if idx > limit:
break
path = directory + '/' + file
image = Image.open(path).resize((width, height)).convert('RGB').transpose(Image.FLIP_TOP_BOTTOM)
with open(f'generated/frame-{idx}.gen.conf', 'w') as frame:
for y in range(height):
for x in range(width):
col = to_ansi(image.getpixel((x, y)))
if prev_image:
old_col = to_ansi(prev_image.getpixel((x, y)))
if col == old_col:
continue
frame.write(f'renamew -t {y}:={x} {col}\n')
frame.write(f'run -d 0.5 -bC "source-file \'generated/frame-{idx + 1}.gen.conf\'"')
prev_image = image
directory = sys.argv[1]
width = int(sys.argv[2]) // 2 - 1
height = int(sys.argv[3])
print(f"using {width}x{height} = {width * height} windows")
output = 'create-windows.gen.conf'
with open(output, 'w') as f:
f.write('set-hook -g session-created {\n')
for _ in range(width):
f.write(' new-window "exit"\n')
f.write(' select-window -t :=0\n')
f.write('}')
print(f"converting images in {directory}...")
compile_images(directory, 200, width, height)