Created
May 22, 2023 19:04
-
-
Save GammaGames/61f97789b43bf2cb0a2475d72651827c to your computer and use it in GitHub Desktop.
Process png files to a json file
This file contains 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
{ | |
"objects": { | |
"#000000": { | |
"image": "/assets/images/rock" | |
} | |
}, | |
"#ac3232": { | |
"player": {} | |
} | |
} |
This file contains 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
{ | |
"objects": [ | |
{ | |
"image": "/assets/images/rock", | |
"x": 96, | |
"y": 176 | |
}, | |
{ | |
"image": "/assets/images/rock", | |
"x": 128, | |
"y": 176 | |
}, | |
{ | |
"image": "/assets/images/rock", | |
"x": 160, | |
"y": 176 | |
}, | |
{ | |
"image": "/assets/images/rock", | |
"x": 96, | |
"y": 192 | |
}, | |
{ | |
"image": "/assets/images/rock", | |
"x": 112, | |
"y": 192 | |
}, | |
{ | |
"image": "/assets/images/rock", | |
"x": 128, | |
"y": 192 | |
}, | |
{ | |
"image": "/assets/images/rock", | |
"x": 160, | |
"y": 192 | |
}, | |
{ | |
"image": "/assets/images/rock", | |
"x": 96, | |
"y": 208 | |
}, | |
{ | |
"image": "/assets/images/rock", | |
"x": 128, | |
"y": 208 | |
}, | |
{ | |
"image": "/assets/images/rock", | |
"x": 160, | |
"y": 208 | |
} | |
], | |
"player": { | |
"x": 128, | |
"y": 128 | |
} | |
} |
This file contains 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
import json | |
import click | |
from PIL import Image | |
@click.group() | |
def cli(): | |
pass | |
@cli.command() | |
@click.argument("file_path", type=click.Path(exists=True)) | |
@click.argument("output_file_path") | |
@click.option("--config-path", "-c", default="./support/maps/config.json", type=click.Path(exists=True)) | |
@click.option("--scale", "-s", default=16, type=int) | |
def map(file_path, output_file_path, config_path, scale): | |
img = Image.open(file_path) | |
pixels = img.load() | |
width, height = img.size | |
config = json.load(open(config_path)) | |
result = {key: [] for key in config if not key.startswith("#")} | |
for y in range(height): | |
for x in range(width): | |
x_pos = x * scale | |
y_pos = y * scale | |
r, g, b, a = pixels[x, y] | |
hex = f"#{r:02x}{g:02x}{b:02x}" | |
if a == 255: | |
found = False | |
if hex in config: | |
found = True | |
for key in config[hex]: | |
object = config[hex][key].copy() | |
object["x"] = x_pos | |
object["y"] = y_pos | |
result[key] = object | |
if not found: | |
for key in result: | |
if key in config and hex in config[key]: | |
found = True | |
object = config[key][hex].copy() | |
object["x"] = x_pos | |
object["y"] = y_pos | |
result[key].append(object) | |
if not found: | |
print(f"Unknown color: {hex} at ({x}, {y})") | |
with open(output_file_path, "w") as f: | |
json.dump(result, f, indent=4) | |
if __name__ == "__main__": | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the source png: