Created
July 5, 2026 05:38
-
-
Save jweinst1/0accadc4ac09bb60d40ae074d8f9095e to your computer and use it in GitHub Desktop.
generates C++ SDL pure texture byte arrays for .png files for pokemon sprites
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from PIL import Image | |
| import sys | |
| BOT_GEN_CODE = "\n}\n" | |
| ALL_BLACK_PX = (0, 0, 0) | |
| ALL_GRAY_PX = (127, 127, 127) | |
| ALL_WHITE_PX = (255, 255, 255) | |
| """ | |
| static constexpr SDL_Color PLAYER_PALT[4] = { | |
| { 0, 0, 0, 0 }, // Index 0: Alpha is 0 (Transparent background) | |
| { 0, 0, 0, 255 }, // Index 1: Opaque Black | |
| { 127, 127, 127, 255 }, // gray opaque | |
| { 255, 255, 255, 255 } // Index 2: Opaque White | |
| }; | |
| """ | |
| # todo function that just maps pixel to a C++ palette | |
| def pix_dist(lfs, rfs): | |
| return abs(lfs[0] - rfs[0]) + abs(lfs[1] - rfs[1]) + abs(lfs[2] - rfs[2]) | |
| def determine_gscale_px(input_px): | |
| dist_dict = {pix_dist(input_px, ALL_BLACK_PX):'1', pix_dist(input_px, ALL_GRAY_PX):'2', pix_dist(input_px, ALL_WHITE_PX):'3'} | |
| return dist_dict[min(dist_dict.keys())] | |
| def map_to_pal(input_px): | |
| if input_px[0] != input_px[1] or input_px[1] != input_px[2] or input_px[2] != input_px[0]: | |
| return '0' | |
| return determine_gscale_px(input_px) | |
| def determine_color_set(imgobj, color_prof): | |
| pxset = set() | |
| img = imgobj.convert(color_prof) | |
| width, height = img.size | |
| for y in range(height): | |
| for x in range(width): | |
| pxset.add(img.getpixel((x, y))) | |
| return pxset | |
| # Open the PNG image | |
| image_path = sys.argv[1] | |
| cpp_var_name = sys.argv[2] | |
| my_var_string = f"static constexpr uint8_t {cpp_var_name}[] = {{\n" | |
| my_gen_strings = [] | |
| with Image.open(image_path) as img: | |
| print(determine_color_set(img, "RGB")) | |
| img = img.convert('RGB') | |
| width, height = img.size | |
| for y in range(height): | |
| my_row = [] | |
| for x in range(width): | |
| my_row.append(map_to_pal(img.getpixel((x, y)))) | |
| my_gen_strings.append(", ".join(my_row)) | |
| full_string = my_var_string + ",\n".join(my_gen_strings) + BOT_GEN_CODE | |
| print(full_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment