Update utils
This commit is contained in:
parent
9c06abdb78
commit
da59527d75
@ -2,6 +2,7 @@ from PIL import Image
|
||||
from path import Path
|
||||
from enum import Enum
|
||||
from itertools import chain
|
||||
from collections import defaultdict
|
||||
import copy
|
||||
import json
|
||||
|
||||
@ -34,10 +35,19 @@ METADATA = {
|
||||
|
||||
def convert_folder(src: Path, dst: Path):
|
||||
dst.mkdir_p()
|
||||
sprite_sizes = set(Image.open(src/path).size for path in chain(*FILENAMES.values()))
|
||||
assert(len(sprite_sizes) == 1)
|
||||
sprite_size = sprite_sizes.pop()
|
||||
assert(sprite_size[0] == sprite_size[1])
|
||||
sprite_sizes = defaultdict(list)
|
||||
for path in chain(*FILENAMES.values()):
|
||||
sprite_sizes[Image.open(src/path).size].append(path)
|
||||
if len(sprite_sizes) != 1:
|
||||
print(f"Error : mixed sprite sizes")
|
||||
for size, files in sprite_sizes.items():
|
||||
print(f"{size} : ")
|
||||
for file in files:
|
||||
print(f"\t- {file}")
|
||||
exit(1)
|
||||
sprite_size = sprite_sizes.keys()[0]
|
||||
if sprite_size[0] != sprite_size[1]:
|
||||
print(f"Error : Non-square sprite size {sprite_size}")
|
||||
size = sprite_size[0]
|
||||
marker_json = {
|
||||
"name": src.name,
|
||||
|
@ -5,23 +5,21 @@ import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("files", type=Path, nargs="+")
|
||||
parser.add_argument("--columns", type=int)
|
||||
args = parser.parse_args()
|
||||
|
||||
images = [Image.open(file) for file in args.files]
|
||||
assert len(set(image.size for image in images)) == 1
|
||||
sprite_size = images[0].size
|
||||
sheet_square_side = ceil(sqrt(len(images)))
|
||||
sheet = Image.new(
|
||||
'RGBA',
|
||||
(
|
||||
sheet_square_side*sprite_size[0],
|
||||
sheet_square_side*sprite_size[1]
|
||||
),
|
||||
(0,0,0,0)
|
||||
)
|
||||
|
||||
columns = args.columns or ceil(sqrt(len(images)))
|
||||
rows = len(images) // columns
|
||||
width = columns * sprite_size[0]
|
||||
height = rows * sprite_size[1]
|
||||
sheet = Image.new('RGBA', (width, height), (0,0,0,0))
|
||||
for index, sprite in enumerate(images):
|
||||
x = (index % sheet_square_side)*sprite_size[0]
|
||||
y = (index // sheet_square_side)*sprite_size[1]
|
||||
x = (index % columns)*sprite_size[0]
|
||||
y = (index // columns)*sprite_size[1]
|
||||
sheet.paste(sprite, (x,y))
|
||||
sheet.save("sheet.png")
|
||||
|
||||
|
46
utils/spritesheet_spliter.py
Normal file
46
utils/spritesheet_spliter.py
Normal file
@ -0,0 +1,46 @@
|
||||
from itertools import product
|
||||
from math import sqrt, ceil
|
||||
from PIL import Image
|
||||
from path import Path
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("file", type=Path)
|
||||
parser.add_argument("sprite_size", type=int)
|
||||
parser.add_argument("-o", "--output-folder", type=Path)
|
||||
parser.add_argument("--name-prefix", default="")
|
||||
parser.add_argument("--name-pattern", default="{i:0{n}}{ext}")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.output_folder is not None:
|
||||
args.output_folder.mkdir_p()
|
||||
else:
|
||||
args.output_folder = Path("./")
|
||||
assert args.sprite_size > 0, f"Invalid sprite_size : {args.sprite_size}"
|
||||
image = Image.open(args.file)
|
||||
width, height = image.size
|
||||
if width % args.sprite_size != 0:
|
||||
print(f"Error : image width ({width}) is not a multiple of {args.sprite_size}")
|
||||
exit(1)
|
||||
if height % args.sprite_size != 0:
|
||||
print(f"Error : image height ({height}) is not a multiple of {args.sprite_size}")
|
||||
exit(1)
|
||||
number_of_sprites = (width // args.sprite_size) * (height // args.sprite_size)
|
||||
for i, (y, x) in enumerate(product(range(height // args.sprite_size), range(width // args.sprite_size))):
|
||||
sprite = image.crop(
|
||||
box=(
|
||||
x*args.sprite_size,
|
||||
y*args.sprite_size,
|
||||
(x+1)*args.sprite_size,
|
||||
(y+1)*args.sprite_size
|
||||
)
|
||||
)
|
||||
sprite.save(
|
||||
args.output_folder/
|
||||
args.name_prefix+
|
||||
args.name_pattern.format(
|
||||
i=i,
|
||||
n=len(str(number_of_sprites)),
|
||||
ext=args.file.ext
|
||||
)
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user