Difficult-Rocket/Difficult_Rocket/unpack_textures.py

62 lines
1.8 KiB
Python
Raw Normal View History

2021-09-08 23:38:34 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2021 by shenjackyuanjie
# All rights reserved
# -------------------------------
"""
2021-02-14 19:03:36 +08:00
writen by shenjackyuanjie
2021-09-08 23:38:34 +08:00
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
2021-02-14 19:03:36 +08:00
2021-09-08 23:38:34 +08:00
from Difficult_Rocket.api import tools
2021-02-17 19:04:22 +08:00
import os
2021-04-03 12:15:24 +08:00
2021-02-17 19:04:22 +08:00
import PIL.Image
2021-04-03 12:15:24 +08:00
import json5
2021-02-14 19:03:36 +08:00
def rewrite_config(name, save_name):
load_xml = tools.load_file(name)
load_xml = load_xml.documentElement
sprites = load_xml.getElementsByTagName('sprite')
pic_path = load_xml.getAttribute('imagePath')
2021-02-17 19:04:22 +08:00
poise = {'image_name': pic_path, 'images': {}}
for sprite in sprites:
poi = tools.get_At(['x', 'y', 'w', 'h'], sprite, int)
poi.append(tools.get_At('r', sprite, str))
2021-02-17 19:04:22 +08:00
save_image = tools.get_At('n', sprite, str)
if save_image.find('PNG') != -1:
save_image = save_image[:-3] + 'png'
poise['images'][save_image] = poi
with open(save_name, 'w') as file:
json5.dump(poise, file)
2021-02-17 19:04:22 +08:00
def cut_and_save(config, save_path):
with open(config) as con:
configs = json5.load(con)
pic = PIL.Image.open('textures/' + configs['image_name'])
try:
os.mkdir('textures/' + save_path)
except Exception as exp:
print(exp)
for config_ in configs['images']:
config__ = configs['images'][config_]
2021-02-22 21:32:13 +08:00
save_name = 'textures/%s/%s' % (save_path, config_)
2021-02-17 19:04:22 +08:00
x, y, w, h, t = config__[0], config__[1], config__[2], config__[3], config__[4]
crop_box = [x, y, x + w, y + h]
pic_ = pic.crop(crop_box)
if t == 'y':
pic_ = pic_.rotate(90, expand=True)
print(save_name)
pic_.save(save_name)
2021-02-20 00:17:50 +08:00
def All_in_one_cut(xml, path):
2021-02-17 19:04:22 +08:00
json_name = xml[:-4] + '.json5'
rewrite_config(xml, json_name)
cut_and_save(json_name, path)