Browse Source

Python script for importing assets from the CCG pack.

GitOrigin-RevId: ee77a84b9b
master
Colin McMillen 2 years ago
parent
commit
a240f0dc31
  1. 62
      tools/assets/import_ccg.py

62
tools/assets/import_ccg.py

@ -0,0 +1,62 @@
#!/usr/bin/env python3
import glob
import os
import pprint # pylint: disable=unused-import
import sys
from PIL import Image
TRANSPARENT_RGBA = (255, 255, 255, 0)
def main(args):
if len(args) < 2:
print('usage: python3 import_ccg.py CCG_ROOT Sprite_Name\n')
print('Add an additional OUTPUT_ROOT argument to actually write output.')
print('(Otherwise, this script just will perform a dry run.)')
return
ccg_root, sprite_name = args[:2]
os.chdir(ccg_root)
output_root = None
if len(args) > 2:
output_root = os.path.join(os.path.expanduser(args[2]), sprite_name)
os.makedirs(output_root, exist_ok=True)
sprite_files = sorted(glob.glob(sprite_name + '/*.png'))
print('sprites: %d' % (len(sprite_files)))
if not sprite_files:
print('no sprite files found!')
return
# Collect sizes.
max_width = 0
max_height = 0
for sprite in sprite_files:
with Image.open(sprite) as image:
max_width = max(max_width, image.width)
max_height = max(max_height, image.height)
new_size = (max_width, max_height)
print('new size: %dx%d' % new_size)
# Now frob them.
for i, sprite in enumerate(sprite_files):
with Image.open(sprite) as image:
top_pad = max_height - image.height
left_pad = (max_width - image.width) // 2
right_pad = max_width - image.width - left_pad
out_filename = '%s_%03d.png' % (sprite_name, i)
print('%03d: %3dx%-3d top: %2d left: %2d right: %2d %s -> %s' % (
i, image.width, image.height, top_pad, left_pad, right_pad, sprite,
out_filename))
if output_root:
new_image = Image.new('RGBA', new_size, TRANSPARENT_RGBA)
new_image.paste(image, (left_pad, top_pad))
new_image.save(os.path.join(output_root, out_filename))
if __name__ == '__main__':
main(sys.argv[1:])
Loading…
Cancel
Save