add actions for checking and annotating sprites

This commit is contained in:
Colin McMillen 2019-10-02 17:37:09 -04:00
parent eeb0f0a4db
commit 4ce860a6ed

View File

@ -3,6 +3,7 @@
import glob
import json
import os
import pprint
import pygame
import re
import sys
@ -237,7 +238,7 @@ def edit_sprite_metadata(filename, metadata=None):
print('invalid choice')
def annotate_sprites(sprite_files, all_metadata):
def annotate_sprites(all_metadata, sprite_files):
pygame.init()
surface = pygame.display.set_mode((1200, 900), pygame.RESIZABLE)
@ -245,23 +246,79 @@ def annotate_sprites(sprite_files, all_metadata):
sprite_metadata, quit = edit_sprite_metadata(
filename, all_metadata.get(filename))
all_metadata[filename] = sprite_metadata
with open('metadata.json', 'w') as f:
with open('sprites.json', 'w') as f:
json.dump(all_metadata, f, sort_keys=True, indent=2)
if quit:
return
def get_named_sprites(metadata):
result = {}
for filename in metadata:
sprite = metadata[filename]
for chunk in sprite.get('chunks', []):
name = chunk.get('name', '')
if not name:
continue
if name in result:
print('warning: duplicated sprite name ', name)
sys.exit(1)
result[name] = chunk
return result
def check_sprites(metadata):
named_sprites = get_named_sprites(metadata)
print('# named sprites:', len(named_sprites))
with open('sprites.json', 'w') as f:
json.dump(metadata, f, sort_keys=True, indent=2)
def stitch_sprites(metadata, filename_base):
sprites = get_named_sprites(metadata)
max_height = 0
total_width = 0
for sprite_name, sprite in sprites.items():
total_width += sprite['width']
max_height = max(max_height, sprite['height'])
print('\n# named sprites:', len(sprites))
print('result will be %dx%d' % (total_width, max_height))
output = pygame.Surface((total_width, max_height), pygame.SRCALPHA)
output_json = {}
xpos = 0
for sprite_name, sprite in sprites.items():
sprite_image = pygame.image.load(sprite['filename'])
area = pygame.Rect(
sprite['x'], sprite['y'], sprite['width'], sprite['height'])
output_json[sprite_name] = {
'name': sprite['name'],
'x': xpos,
'y': 0,
'width': sprite['width'],
'height': sprite['height']
}
output.blit(sprite_image, (xpos, 0), area)
xpos += sprite['width']
image_filename = os.path.expanduser(filename_base) + '.png'
print('saving image to', image_filename)
pygame.image.save(output, image_filename)
json_filename = os.path.expanduser(filename_base) + '.json'
print('saving json to', json_filename)
with open(json_filename, 'w') as json_file:
json.dump(output_json, json_file, sort_keys=True, indent=2)
def main(args):
os.chdir(os.path.expanduser('~/time_fantasy'))
with open('metadata.json') as f:
all_metadata = json.load(f)
sprite_files = unglob(SPRITE_FILES)
tileset_files = unglob(TILESET_FILES)
animation_files = unglob(ANIMATION_FILES)
icon_files = unglob(ICON_FILES)
background_files = unglob(BACKGROUND_FILES)
print('sprites: %d tilesets: %d animations: %d icons: %d backgrounds: %d' %
print('\nsprites: %d tilesets: %d animations: %d icons: %d backgrounds: %d' %
(len(sprite_files), len(tileset_files), len(animation_files),
len(icon_files), len(background_files)))
@ -270,8 +327,21 @@ def main(args):
command = args[0]
with open('sprites.json') as f:
metadata = json.load(f)
if command == 'annotate-sprites':
annotate_sprites(sprite_files, all_metadata)
annotate_sprites(metadata, sprite_files)
elif command == 'check-sprites':
check_sprites(metadata)
elif command == 'stitch-sprites':
if len(args) < 2:
print('need FILENAME_BASE')
return
filename_base = args[1]
stitch_sprites(metadata, filename_base)
else:
print('unrecognized command "%s"' % command)
if __name__ == '__main__':