diff options
| author | yum <yum.food.vr@gmail.com> | 2023-02-02 01:02:03 -0800 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2023-02-13 14:36:20 -0800 |
| commit | 7c6894614dcc3ebc5d4c8839b64f4da761b5ccf0 (patch) | |
| tree | 6232b86b09190fd162aeb67229da359971b2e517 /Scripts/emotes_v2.py | |
| parent | 2fc3b1b978b6e24814e9de7200865b912108bd34 (diff) | |
Begin work adding emotes
Done:
* Users can add images to Fonts/Emotes/
* The basename of that image ('clueless.png' becomes 'clueless') is the
keyword to make the image show up in game.
* Fix a bug in the shader where letters on the 2nd texture and later
would have UV outside of [0.0, 1.0]
Not yet implemented:
* transcribed words are encoded using emotes mapping
Diffstat (limited to 'Scripts/emotes_v2.py')
| -rw-r--r-- | Scripts/emotes_v2.py | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/Scripts/emotes_v2.py b/Scripts/emotes_v2.py new file mode 100644 index 0000000..195e116 --- /dev/null +++ b/Scripts/emotes_v2.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 + +import argparse +import os +import pickle +import sys + +from math import floor +from PIL import Image +from typing import Any, Dict, List, Tuple + +# The character range [0x0000, 0xDFFF] is reserved for text. +# The range [0xE000, infinity) is left over for emotes. +EMOTES_LETTER_OFFSET = 0xE000 + +def superimpose_image(base_img: Image, overlay_img: Image, position: Tuple[int, int]) -> Image: + base_img.paste(overlay_img, position, overlay_img) + return base_img + +def i_to_pos(i, sm_wd, sm_ht, big_wd, big_ht) -> Tuple[int, int]: + x = i * sm_wd % big_wd + row = floor((i * sm_wd) / big_wd) + y = row * sm_ht + return int(x), int(y) + +def get_images_from_directory(directory_path: str) -> List[Tuple[Any, str]]: + images = [] + for filename in os.listdir(directory_path): + file_path = os.path.join(directory_path, filename) + if os.path.isfile(file_path) and file_path.endswith(".png"): + image = Image.open(file_path) + name = os.path.basename(filename).split('.')[0] + images.append((image, name)) + return images + +def split_resized_image(img, wd: int, ht: int) -> List[Any]: + aspect_ratio = img.width / img.height + width = int(ht * aspect_ratio) + img = img.resize((width, ht)) + + split_images = [] + for i in range(0, img.width, wd): + split_image = img.crop((i, 0, i + wd, ht)) + split_images.append(split_image) + + return split_images + +print(i_to_pos(0, 5, 10, 10, 20)) +print(i_to_pos(1, 5, 10, 10, 20)) +print(i_to_pos(2, 5, 10, 10, 20)) +print(i_to_pos(3, 5, 10, 10, 20)) + +def resize_image_with_aspect_ratio(img: Image, aspect_ratio: float) -> Image: + original_width, original_height = img.size + new_width = int(original_height * aspect_ratio) + new_height = original_height + return img.resize((new_width, new_height)) + +def resize_image_to_height(img: Image, height: int) -> Image: + aspect_ratio = img.width / img.height + new_width = int(height * aspect_ratio) + return img.resize((new_width, height)) + +class EmotesState: + def __init__(self): + self.bits = {} + + def load(self, pickle_path): + with open(pickle_path, 'rb') as f: + self.bits = pickle.load(f) + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("dir", type=str, help="directory to get images from") + parser.add_argument("board_aspect_ratio", help="aspect ratio of a cell in the board") + parser.add_argument("texture_aspect_ratio", help="aspect ratio of a cell in the texture") + parser.add_argument("tex_path", type=str, help="path to save the texture to") + parser.add_argument("pickle_path", type=str, help="path to save the texture index to") + args = parser.parse_args() + + directory_path = args.dir + board_aspect_ratio = int(args.board_aspect_ratio) + texture_aspect_ratio = int(args.texture_aspect_ratio) + + base_img = Image.new("RGBA", (4096, 4096), (0, 0, 0, 0)) + images_and_filenames = get_images_from_directory(directory_path) + i = 0 + bits = {} # Dict[str, List[int]] + for img, filename in images_and_filenames: + print(f"Adding {filename}") + img = resize_image_with_aspect_ratio(img, board_aspect_ratio) + img = resize_image_to_height(img, 1024) + img_fragments = split_resized_image(img, int(1024 / texture_aspect_ratio), 1024) + img_bits = [] # List[int] + for img_fragment in img_fragments: + i = i + 1 + img_pos = i_to_pos(i, + 1024 / texture_aspect_ratio, 1024, + 4096, 4096) + print(f"{img_pos}") + superimpose_image(base_img, img_fragment, img_pos) + img_bits.append(EMOTES_LETTER_OFFSET + i) + emote_name = os.path.basename(filename).split('.')[0] + print(f"{emote_name} -> {img_bits}") + bits[emote_name] = img_bits + base_img.save(args.tex_path) + with open(args.pickle_path, 'wb') as f: + pickle.dump(bits, f) + |
