Created
October 3, 2023 14:45
-
-
Save outloudvi/d0e0f1805ca6a4952d8c2b827b4467b0 to your computer and use it in GitHub Desktop.
Draw areas in an atlas image.
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
from PIL import Image, ImageDraw | |
def parse_atlas_data(text): | |
lines = text.split("\n") | |
line_id = 0 | |
items = [] | |
meta = {} | |
curr_item = None | |
meta_read = False | |
while line_id < len(lines): | |
line = lines[line_id] | |
line_id += 1 | |
if ':' not in line: | |
if curr_item is not None: | |
if not meta_read: | |
meta_read = True | |
meta = curr_item | |
else: | |
items.append(curr_item) | |
curr_item = None | |
if line.strip() != "": | |
curr_item = { | |
"name": line.strip() | |
} | |
else: | |
(key, val) = line.strip().split(":") | |
curr_item[key.strip()] = val.strip() | |
if curr_item is not None: | |
items.append(curr_item) | |
return (meta, items) | |
atlas_data = open("file.atlas", encoding="utf-8").read() | |
im = Image.open("file.png") | |
print(im.format, im.size, im.mode) | |
(_, items) = parse_atlas_data(atlas_data) | |
draw = ImageDraw.Draw(im) | |
for item in items: | |
name = item["name"] | |
(x, y) = list(map(int, item["xy"].split(","))) | |
(sizex, sizey) = list(map(int, item["size"].split(","))) | |
if item["rotate"] == "true": | |
(sizex, sizey) = (sizey, sizex) | |
draw.rectangle((x, y, x + sizex, y + sizey), outline=(0, 255, 255)) | |
draw.text((x,y), name, fill=(0, 0, 0)) | |
im.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment