yum-slop/TaSTT

Free self-hosted STT for VRChat.

git clone https://git.yummers.dev/yum-slop/TaSTT

yumBegin work adding emotes7c68946

v0.20.0
5.4 KiB143 linesraw
1#!/usr/bin/env python3
2
3import argparse
4from math import floor
5import os
6# python3 -m pip install pillow
7from PIL import Image
8import sys
9
10# (row, col)
11TEX_SZ = (2048, 2048)
12
13IMG_SZ_PX = 256
14IMG_PER_ROW = int(TEX_SZ[0] / IMG_SZ_PX)
15IMG_PER_COL = int(TEX_SZ[1] / IMG_SZ_PX)
16
17# TODO(yum) this should live in a config file.
18# Note: the name of the emote must be no longer than 6 characters.
19IMG_TEX_DATA = []
20IMG_TEX_DATA.append(("Images/Emotes/xdd.png", "xdd"))
21IMG_TEX_DATA.append(("Images/Emotes/pog.png", "pog"))
22IMG_TEX_DATA.append(("Images/Emotes/lulw.png", "laugh"))
23IMG_TEX_DATA.append(("Images/Emotes/bighardo.png", "hard"))
24IMG_TEX_DATA.append(("Images/Emotes/peepoHappy.png", "happy"))
25IMG_TEX_DATA.append(("Images/Emotes/peepoSad.png", "sad"))
26IMG_TEX_DATA.append(("Images/Emotes/bedge.png", "bed"))
27IMG_TEX_DATA.append(("Images/Emotes/reallymad.png", "mad"))
28IMG_TEX_DATA.append(("Images/Emotes/clueless.png", "surely"))
29IMG_TEX_DATA.append(("Images/Emotes/what.png", "what"))
30IMG_TEX_DATA.append(("Images/Emotes/based.png", "based"))
31IMG_TEX_DATA.append(("Images/Emotes/chad.png", "chad"))
32IMG_TEX_DATA.append(("Images/Emotes/aware.png", "aware"))
33IMG_TEX_DATA.append(("Images/Emotes/girl.png", "girl"))
34IMG_TEX_DATA = []
35
36IMG_TEX_KEYWORD_TO_COORD = {}
37for i in range(0, len(IMG_TEX_DATA)):
38    IMG_TEX_KEYWORD_TO_COORD[IMG_TEX_DATA[i][1]] = i
39
40# We treat images like words. To keep things simple, they're the same height as
41# a word, and they're a fixed width.
42IMG_SZ_LETTER_ROWS = 1
43IMG_SZ_LETTER_COLS = 6
44
45def lookup(word: str):
46    word = word.lower()
47    word = ''.join(c for c in word.lower() if c.isalpha())
48    if word in IMG_TEX_KEYWORD_TO_COORD.keys():
49        return word, IMG_TEX_KEYWORD_TO_COORD[word]
50    return None, None
51
52def openTexture(tex_path: str):
53    if not os.path.exists(args.texture_path):
54        return Image.new("RGB", TEX_SZ)
55    tex = Image.open(args.texture_path)
56    if tex.size != TEX_SZ:
57        print("Texture at {} has mismatching size {}, creating new texture".format(
58            tex_path, tex.size), file=sys.stderr)
59        return Image.new("RGB", TEX_SZ)
60    return tex
61
62# Add an image to the texture at the coordinates (x, y). x and y should be in
63# the range [0, IMG_PER_COL) and [0, IMG_PER_ROW) respectively.
64def addImageToTexture(tex: Image, img_path: str, x: int, y:int):
65    # Transparent images will be composited on top of a black background.
66    img = Image.open(img_path).convert('RGBA')
67    img_bg = Image.new('RGBA', img.size, (0, 0, 0))
68    img = Image.alpha_composite(img_bg, img).convert('RGB')
69
70    max_px = IMG_SZ_PX
71
72    # Scale the image up so it uses as much space as is given to it.
73    # I originally planned to support multiple scales, but this proved to be
74    # too much work - getting line wrapping to work with this would be a pain.
75    # So for now, all images are the same height as words.
76    scale = 1
77    img_x, img_y = img.size
78    max_dim = max(img_x, img_y)
79    img_scale = (max_px / max_dim) * scale
80    new_sz = (int(floor(img.size[0] * img_scale)),
81            int(floor(img.size[1] * img_scale)))
82    print("Add image {}".format(img_path))
83    print("  Original size: {}".format(img.size))
84    print("  Scaled size: {}".format(new_sz))
85    img = img.resize(new_sz)
86
87    # Center the image within its new coordinate space.
88    padded_img_sz = (IMG_SZ_PX * scale, IMG_SZ_PX * scale)
89    padded_img = Image.new("RGB", padded_img_sz)
90    centered_x = int(floor((padded_img_sz[0] - new_sz[0]) / 2))
91    centered_y = int(floor((padded_img_sz[1] - new_sz[1]) / 2))
92    padded_img.paste(img, box=(centered_x, centered_y))
93    img = padded_img
94
95    # Break the image into tiles and write them into the texture.
96    for slot in range(0, scale * scale):
97        tile_x = slot % scale
98        tile_y = int(floor(slot / scale))
99        tile_bbox = (tile_x * IMG_SZ_PX, tile_y * IMG_SZ_PX, (tile_x + 1) * IMG_SZ_PX, (tile_y + 1) * IMG_SZ_PX)
100        tile = img.crop(tile_bbox)
101        print("  tile {},{} (bbox={})".format(tile_x, tile_y, tile_bbox))
102
103        slot_x = x + slot % IMG_PER_ROW
104        slot_y = y + int(floor(slot / IMG_PER_ROW))
105        slot_x_px = slot_x * IMG_SZ_PX
106        slot_y_px = slot_y * IMG_SZ_PX
107        print("  Add img at {},{} (px {},{})".format(slot_x, slot_y, slot_x_px, slot_y_px))
108
109        tex.paste(tile, box=(slot_x_px, slot_y_px))
110
111def parseArgs():
112    parser = argparse.ArgumentParser()
113    parser.add_argument("--texture_path", type=str, help="Path to save the generated texture.")
114    parser.add_argument("--rows", type=str, help="The number of rows on the board")
115    parser.add_argument("--cols", type=str, help="The number of columns on the board")
116    args = parser.parse_args()
117
118    if not args.texture_path or not args.rows or not args.cols:
119        print("--texture_path, --rows, --cols required", file=sys.stderr)
120        sys.exit(1)
121
122    return args
123
124if __name__ == "__main__":
125    args = parseArgs()
126
127    rows = int(args.rows)
128    cols = int(args.cols)
129    # board is this much wider than tall
130    board_aspect_ratio = 2
131    # each cell a square divided into `rows`x`cols` is this much wider than tall
132    cell_aspect_ratio = rows / cols
133    # each cell is this much wider than tall
134    board_cell_aspect_ratio = board_aspect_ratio * cell_aspect_ratio
135
136    tex = openTexture(args.texture_path)
137    for i in range(0, len(IMG_TEX_DATA)):
138        filename = IMG_TEX_DATA[i][0]
139        x = i % IMG_PER_ROW
140        y = int(floor(i / IMG_PER_ROW))
141        addImageToTexture(tex, filename, x, y)
142    tex.save(args.texture_path)
143