Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for CSVs, as well as callback functions as the atlas_format #19

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions PyTexturePacker/PackerInterface/AtlasInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions PyTexturePacker/PackerInterface/PackerInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)))
Expand Down
40 changes: 38 additions & 2 deletions PyTexturePacker/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
10 changes: 9 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
==========

Expand Down