yum-slop/TaSTT
Free self-hosted STT for VRChat.
git clone https://git.yummers.dev/yum-slop/TaSTT
7c68946
v0.21.0
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 )) : 38IMG_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 ) : 46word = word . lower () 47word = '' . join ( c for c in word . lower () if c . isalpha ()) 48if word in IMG_TEX_KEYWORD_TO_COORD . keys () : 49return word , IMG_TEX_KEYWORD_TO_COORD [ word ] 50return None , None 51 52def openTexture ( tex_path : str ) : 53if not os . path . exists ( args . texture_path ) : 54return Image . new ( "RGB" , TEX_SZ ) 55tex = Image . open ( args . texture_path ) 56if tex . size != TEX_SZ : 57( "Texture at {} has mismatching size {}, creating new texture" . format ( 58tex_path , tex . size ) , file = sys . stderr ) 59return Image . new ( "RGB" , TEX_SZ ) 60return 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. 66img = Image . open ( img_path ) . convert ( 'RGBA' ) 67img_bg = Image . new ( 'RGBA' , img . size , ( 0 , 0 , 0 )) 68img = Image . alpha_composite ( img_bg , img ) . convert ( 'RGB' ) 69 70max_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. 76scale = 1 77img_x , img_y = img . size 78max_dim = max ( img_x , img_y ) 79img_scale = ( max_px / max_dim ) * scale 80new_sz = ( int ( floor ( img . size [ 0 ] * img_scale )) , 81int ( floor ( img . size [ 1 ] * img_scale ))) 82( "Add image {}" . format ( img_path )) 83( " Original size: {}" . format ( img . size )) 84( " Scaled size: {}" . format ( new_sz )) 85img = img . resize ( new_sz ) 86 87# Center the image within its new coordinate space. 88padded_img_sz = ( IMG_SZ_PX * scale , IMG_SZ_PX * scale ) 89padded_img = Image . new ( "RGB" , padded_img_sz ) 90centered_x = int ( floor (( padded_img_sz [ 0 ] - new_sz [ 0 ]) / 2 )) 91centered_y = int ( floor (( padded_img_sz [ 1 ] - new_sz [ 1 ]) / 2 )) 92padded_img . paste ( img , box = ( centered_x , centered_y )) 93img = padded_img 94 95# Break the image into tiles and write them into the texture. 96for slot in range ( 0 , scale * scale ) : 97tile_x = slot % scale 98tile_y = int ( floor ( slot / scale )) 99tile_bbox = ( tile_x * IMG_SZ_PX , tile_y * IMG_SZ_PX , ( tile_x + 1 ) * IMG_SZ_PX , ( tile_y + 1 ) * IMG_SZ_PX ) 100tile = img . crop ( tile_bbox ) 101( " tile {},{} (bbox={})" . format ( tile_x , tile_y , tile_bbox )) 102 103slot_x = x + slot % IMG_PER_ROW 104slot_y = y + int ( floor ( slot / IMG_PER_ROW )) 105slot_x_px = slot_x * IMG_SZ_PX 106slot_y_px = slot_y * IMG_SZ_PX 107( " Add img at {},{} (px {},{})" . format ( slot_x , slot_y , slot_x_px , slot_y_px )) 108 109tex . paste ( tile , box = ( slot_x_px , slot_y_px )) 110 111def parseArgs () : 112parser = argparse . ArgumentParser () 113parser . add_argument ( "--texture_path" , type = str , help = "Path to save the generated texture." ) 114parser . add_argument ( "--rows" , type = str , help = "The number of rows on the board" ) 115parser . add_argument ( "--cols" , type = str , help = "The number of columns on the board" ) 116args = parser . parse_args () 117 118if not args . texture_path or not args . rows or not args . cols : 119( "--texture_path, --rows, --cols required" , file = sys . stderr ) 120sys . exit ( 1 ) 121 122return args 123 124if __name__ == "__main__" : 125args = parseArgs () 126 127rows = int ( args . rows ) 128cols = int ( args . cols ) 129# board is this much wider than tall 130board_aspect_ratio = 2 131# each cell a square divided into `rows`x`cols` is this much wider than tall 132cell_aspect_ratio = rows / cols 133# each cell is this much wider than tall 134board_cell_aspect_ratio = board_aspect_ratio * cell_aspect_ratio 135 136tex = openTexture ( args . texture_path ) 137for i in range ( 0 , len ( IMG_TEX_DATA )) : 138filename = IMG_TEX_DATA [ i ][ 0 ] 139x = i % IMG_PER_ROW 140y = int ( floor ( i / IMG_PER_ROW )) 141addImageToTexture ( tex , filename , x , y ) 142tex . save ( args . texture_path ) 143