diff --git a/PyTexturePacker/PackerInterface/AtlasInterface.py b/PyTexturePacker/PackerInterface/AtlasInterface.py index ddabe82..4d31a3e 100644 --- a/PyTexturePacker/PackerInterface/AtlasInterface.py +++ b/PyTexturePacker/PackerInterface/AtlasInterface.py @@ -9,7 +9,7 @@ AtlasInterface.py ----------------------------------------------------------------------------""" -from ..Utils import ATLAS_FORMAT_PLIST, ATLAS_FORMAT_JSON +from ..Utils import ATLAS_FORMAT_PLIST#, ATLAS_FORMAT_JSON MAX_RANK = 2 ** 32 MAX_WIDTH = 1024 * 16 @@ -68,8 +68,7 @@ def dump_plist(self, texture_file_name="", input_base_path=None, atlas_format=AT image_rect.source_box[0], image_rect.source_box[1], width, height), sourceSize="{%d,%d}" % image_rect.source_size, ) - - if atlas_format == ATLAS_FORMAT_JSON: + else: frames[path] = dict( frame=dict(x=image_rect.x, y=image_rect.y, w=width, h=height), rotated=bool(image_rect.rotated), @@ -88,8 +87,7 @@ def dump_plist(self, texture_file_name="", input_base_path=None, atlas_format=AT realTextureFileName=texture_file_name, size="{%d,%d}" % self.size, ) - - if atlas_format == ATLAS_FORMAT_JSON: + else: plist_data["meta"] = dict( image=texture_file_name, format="RGBA8888", diff --git a/PyTexturePacker/PackerInterface/PackerInterface.py b/PyTexturePacker/PackerInterface/PackerInterface.py index 10ced70..b6114d8 100644 --- a/PyTexturePacker/PackerInterface/PackerInterface.py +++ b/PyTexturePacker/PackerInterface/PackerInterface.py @@ -34,7 +34,7 @@ class PackerInterface(object): def __init__(self, bg_color=0x00000000, texture_format=".png", max_width=4096, max_height=4096, enable_rotated=True, force_square=False, border_padding=2, shape_padding=2, inner_padding=0, trim_mode=0, - reduce_border_artifacts=False, extrude=0, atlas_format=Utils.ATLAS_FORMAT_PLIST): + reduce_border_artifacts=False, extrude=0, atlas_format=Utils.ATLAS_FORMAT_PLIST, atlas_ext=None): """ init a packer :param bg_color: background color of output image. @@ -65,6 +65,7 @@ def __init__(self, bg_color=0x00000000, texture_format=".png", max_width=4096, m self.trim_mode = trim_mode self.reduce_border_artifacts = reduce_border_artifacts self.atlas_format = atlas_format + self.atlas_ext = atlas_ext @staticmethod def _calculate_area(image_rect_list, inner_padding): @@ -194,7 +195,7 @@ def pack(self, input_images, output_name, output_path="", input_base_path=None): if self.reduce_border_artifacts: packed_image = Utils.alpha_bleeding(packed_image) - atlas_data_ext = Utils.get_atlas_data_ext(self.atlas_format) + atlas_data_ext = self.atlas_ext or Utils.get_atlas_data_ext(self.atlas_format) Utils.save_atlas_data(packed_plist, os.path.join(output_path, "%s%s" % (texture_file_name, atlas_data_ext)), self.atlas_format) Utils.save_image(packed_image, os.path.join(output_path, "%s%s" % (texture_file_name, self.texture_format))) diff --git a/PyTexturePacker/Utils.py b/PyTexturePacker/Utils.py index 93cff8e..d91b3b7 100644 --- a/PyTexturePacker/Utils.py +++ b/PyTexturePacker/Utils.py @@ -9,12 +9,14 @@ Utils.py ----------------------------------------------------------------------------""" import sys +import inspect if sys.version_info.major > 2: xrange = range SUPPORTED_IMAGE_FORMAT = [".png", ".jpg", ".bmp"] ATLAS_FORMAT_PLIST = "plist" ATLAS_FORMAT_JSON = "json" +ATLAS_FORMAT_CSV = "csv" def load_images_from_paths(image_path_list): @@ -60,8 +62,17 @@ def get_atlas_data_ext(atlas_format): """ if atlas_format == ATLAS_FORMAT_PLIST: return '.plist' - if atlas_format == ATLAS_FORMAT_JSON: + elif atlas_format == ATLAS_FORMAT_JSON: return '.json' + elif atlas_format == ATLAS_FORMAT_CSV: + return '.csv' + elif callable(atlas_format): + parameters = inspect.signature(atlas_format).parameters + required_args = sum(1 for param in parameters.values() if param.default is param.empty) + if len(parameters) >= 2 and required_args <= 2: + return '.txt' + + raise ValueError(f"Unsupported file format: {atlas_format}") def save_atlas_data(data_dict, file_path, atlas_format): @@ -74,8 +85,33 @@ def save_atlas_data(data_dict, file_path, atlas_format): """ if atlas_format == ATLAS_FORMAT_PLIST: return save_plist(data_dict, file_path) - if atlas_format == ATLAS_FORMAT_JSON: + elif atlas_format == ATLAS_FORMAT_JSON: return save_json(data_dict, file_path) + elif atlas_format == ATLAS_FORMAT_CSV: + return save_csv(data_dict, file_path) + elif callable(atlas_format): + parameters = inspect.signature(atlas_format).parameters + required_args = sum(1 for param in parameters.values() if param.default is param.empty) + if len(parameters) >= 2 and required_args <= 2: + return atlas_format(data_dict, file_path) + + raise ValueError(f"Unsupported file format: {atlas_format}") + + +def save_csv(data_dict, file_path): + """ + save a dict as a csv + :param data_dict: dict data + :param file_path: csv file path to save + :return: + """ + with open(file_path, 'w') as fp: + for name, data in data_dict['frames'].items(): + frame = data['frame'] + source = data['spriteSourceSize'] + fp.write(f'{name},{frame["x"]},{frame["y"]},{frame["w"]},{frame["h"]},' + f'{source["x"]},{source["y"]},{source["w"]},{source["h"]},' + f'{data["rotated"]},{data["trimed"]}\n') def save_json(data_dict, file_path): diff --git a/README.rst b/README.rst index a386b85..6865f63 100644 --- a/README.rst +++ b/README.rst @@ -128,6 +128,7 @@ These color values can reduce artifacts around sprites and removes dark halos at extrude ------- + Extrude repeats the sprite's pixels at the border. Sprite's size is not changed. There are two uses for this: @@ -137,9 +138,16 @@ There are two uses for this: atlas_format ------- -Choose the texture config format that file will use. Available options "plist" or "json". + +Choose the texture config format that file will use. Available options "plist", "json" and "csv". Aditionally, you can use a custom function that will receive a dictionary and a path and handle the format in some custom way. The default texture config output format is "plist". +atlas_ext +------- + +Forces the atlas to use this extension regardless of the format. +If not provided, the atlas will use the default extension for the chosen format. + Contribute ==========