From 9c8f28c89183f803ee3af5203691a8a9a8a68a69 Mon Sep 17 00:00:00 2001 From: huang quanyong Date: Wed, 14 Feb 2024 21:58:11 +0800 Subject: [PATCH] mod format code & mod README.rst --- .../GuillotinePacker/GuillotineAtlas.py | 6 ++-- PyTexturePacker/ImageRect.py | 3 +- .../MaxRectsPacker/MaxRectsAtlas.py | 27 +++++++++-------- .../MaxRectsPacker/MaxRectsPacker.py | 6 ++-- .../PackerInterface/AtlasInterface.py | 17 +++++++---- .../PackerInterface/PackerInterface.py | 30 +++++++++++-------- PyTexturePacker/Rect.py | 1 - PyTexturePacker/Utils.py | 23 +++++++------- PyTexturePacker/__init__.py | 10 +++---- README.rst | 9 +++--- docs/1_quick_start.rst | 2 +- main.py | 2 +- 12 files changed, 75 insertions(+), 61 deletions(-) diff --git a/PyTexturePacker/GuillotinePacker/GuillotineAtlas.py b/PyTexturePacker/GuillotinePacker/GuillotineAtlas.py index 111ca15..19504c9 100644 --- a/PyTexturePacker/GuillotinePacker/GuillotineAtlas.py +++ b/PyTexturePacker/GuillotinePacker/GuillotineAtlas.py @@ -50,7 +50,8 @@ def cut(self, main_rect, sub_rect): def place_image_rect(self, rect_index, image_rect): rect = self.max_rect_list[rect_index] - image_rect.x, image_rect.y = rect.x + self.inner_padding, rect.y + self.inner_padding + image_rect.x, image_rect.y = rect.x + \ + self.inner_padding, rect.y + self.inner_padding fake_image_rect = image_rect.clone() fake_image_rect.left -= self.inner_padding @@ -61,6 +62,3 @@ def place_image_rect(self, rect_index, image_rect): self.max_rect_list.pop(rect_index) self.max_rect_list.extend(self.cut(rect, fake_image_rect)) self.image_rect_list.append(image_rect) - - - diff --git a/PyTexturePacker/ImageRect.py b/PyTexturePacker/ImageRect.py index 6cfc1ba..9699513 100644 --- a/PyTexturePacker/ImageRect.py +++ b/PyTexturePacker/ImageRect.py @@ -14,7 +14,6 @@ from .Rect import Rect from . import Utils - class ImageRect(Rect): """ image rect data @@ -89,7 +88,7 @@ def trim(self, v=1): self.width, self.height = self.image.size self._trimmed = True - + def extrude(self, size=0): if size <= 0: return diff --git a/PyTexturePacker/MaxRectsPacker/MaxRectsAtlas.py b/PyTexturePacker/MaxRectsPacker/MaxRectsAtlas.py index d68d84f..0c3c52e 100644 --- a/PyTexturePacker/MaxRectsPacker/MaxRectsAtlas.py +++ b/PyTexturePacker/MaxRectsPacker/MaxRectsAtlas.py @@ -39,9 +39,9 @@ def __init__(self, *args, **kwargs): width, height = self.size self.max_rect_list = [Rect(0 + self.border_padding, - 0 + self.border_padding, - width - 2 * self.border_padding, - height - 2 * self.border_padding)] + 0 + self.border_padding, + width - 2 * self.border_padding, + height - 2 * self.border_padding)] def _is_in_max_size(self, size): return size[0] <= self.max_size[0] and size[1] <= self.max_size[1] @@ -83,19 +83,20 @@ def expand(self, method=EXPAND_SHORT_SIDE): if old_size[0] != self.size[0]: new_rect = Rect(old_size[0] - self.border_padding, - 0 + self.border_padding, - self.size[0] - old_size[0], - self.size[1] - 2 * self.border_padding) + 0 + self.border_padding, + self.size[0] - old_size[0], + self.size[1] - 2 * self.border_padding) self.max_rect_list.append(new_rect) if old_size[1] != self.size[1]: new_rect = Rect(0 + self.border_padding, - old_size[1] - self.border_padding, - self.size[0] - 2 * self.border_padding, - self.size[1] - old_size[1]) + old_size[1] - self.border_padding, + self.size[0] - 2 * self.border_padding, + self.size[1] - old_size[1]) self.max_rect_list.append(new_rect) - self.max_rect_list = list(filter(self._max_rect_list_pruning, self.max_rect_list)) + self.max_rect_list = list( + filter(self._max_rect_list_pruning, self.max_rect_list)) return True @@ -183,7 +184,8 @@ def find_best_rank_with_rotate(self, image_rect): def place_image_rect(self, rect_index, image_rect): rect = self.max_rect_list[rect_index] - image_rect.x, image_rect.y = rect.x + self.inner_padding, rect.y + self.inner_padding + image_rect.x, image_rect.y = rect.x + \ + self.inner_padding, rect.y + self.inner_padding fake_image_rect = image_rect.clone() fake_image_rect.left -= self.inner_padding @@ -200,7 +202,8 @@ def place_image_rect(self, rect_index, image_rect): _max_rect_list.append(rect) self.max_rect_list = _new_max_rect_list - self.max_rect_list = list(filter(self._max_rect_list_pruning, _new_max_rect_list)) + self.max_rect_list = list( + filter(self._max_rect_list_pruning, _new_max_rect_list)) self.max_rect_list.extend(_max_rect_list) self.image_rect_list.append(image_rect) diff --git a/PyTexturePacker/MaxRectsPacker/MaxRectsPacker.py b/PyTexturePacker/MaxRectsPacker/MaxRectsPacker.py index 781fd9a..a677ff4 100644 --- a/PyTexturePacker/MaxRectsPacker/MaxRectsPacker.py +++ b/PyTexturePacker/MaxRectsPacker/MaxRectsPacker.py @@ -30,7 +30,8 @@ def __init__(self, *args, **kwargs): def _pack(self, image_rect_list): atlas_list = self._init_atlas_list(image_rect_list) - image_rect_list = sorted(image_rect_list, key=lambda x: max(x.width, x.height), reverse=True) + image_rect_list = sorted(image_rect_list, key=lambda x: max( + x.width, x.height), reverse=True) for image_rect in image_rect_list: best_atlas = -1 @@ -39,7 +40,8 @@ def _pack(self, image_rect_list): best_rotated = False for i, max_rect in enumerate(atlas_list): - index, rank, rotated = max_rect.find_best_rank(image_rect, self.enable_rotated) + index, rank, rotated = max_rect.find_best_rank( + image_rect, self.enable_rotated) if rank < best_rank: best_atlas = i diff --git a/PyTexturePacker/PackerInterface/AtlasInterface.py b/PyTexturePacker/PackerInterface/AtlasInterface.py index 4d31a3e..af0beaf 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 @@ -57,11 +57,13 @@ def dump_plist(self, texture_file_name="", input_base_path=None, atlas_format=AT if input_base_path is None: _, path = os.path.split(path) else: - path = os.path.relpath(os.path.abspath(path), os.path.abspath(input_base_path)) + path = os.path.relpath(os.path.abspath( + path), os.path.abspath(input_base_path)) if atlas_format == ATLAS_FORMAT_PLIST: frames[path] = dict( - frame="{{%d,%d},{%d,%d}}" % (image_rect.x, image_rect.y, width, height), + frame="{{%d,%d},{%d,%d}}" % ( + image_rect.x, image_rect.y, width, height), offset="{%d,%d}" % center_offset, rotated=bool(image_rect.rotated), sourceColorRect="{{%d,%d},{%d,%d}}" % ( @@ -70,13 +72,15 @@ def dump_plist(self, texture_file_name="", input_base_path=None, atlas_format=AT ) else: frames[path] = dict( - frame=dict(x=image_rect.x, y=image_rect.y, w=width, h=height), + frame=dict(x=image_rect.x, y=image_rect.y, + w=width, h=height), rotated=bool(image_rect.rotated), trimed=bool(image_rect.trimmed), spriteSourceSize=dict( x=image_rect.source_box[0], y=image_rect.source_box[1], w=image_rect.source_box[2], h=image_rect.source_box[3]), - sourceSize=dict(w=image_rect.source_size[0], h=image_rect.source_size[1]) + sourceSize=dict( + w=image_rect.source_size[0], h=image_rect.source_size[1]) ) plist_data["frames"] = frames @@ -105,6 +109,7 @@ def dump_image(self, bg_color=0xffffffff): image = image_rect.image.crop() if image_rect.rotated: image = image.transpose(Image.ROTATE_270) - packed_image.paste(image, (image_rect.left, image_rect.top, image_rect.right, image_rect.bottom)) + packed_image.paste( + image, (image_rect.left, image_rect.top, image_rect.right, image_rect.bottom)) return packed_image diff --git a/PyTexturePacker/PackerInterface/PackerInterface.py b/PyTexturePacker/PackerInterface/PackerInterface.py index b6114d8..a9791b6 100644 --- a/PyTexturePacker/PackerInterface/PackerInterface.py +++ b/PyTexturePacker/PackerInterface/PackerInterface.py @@ -72,9 +72,9 @@ def _calculate_area(image_rect_list, inner_padding): area = 0 for image_rect in image_rect_list: area += image_rect.area + \ - image_rect.width * inner_padding + \ - image_rect.height * inner_padding + \ - inner_padding ** 2 + image_rect.width * inner_padding + \ + image_rect.height * inner_padding + \ + inner_padding ** 2 return area @staticmethod @@ -131,7 +131,7 @@ def _init_atlas_list(self, image_rect_list): if self.enable_rotated: if min(min_width, min_height) > min(self.max_width, self.max_height) or \ - max(min_width, min_height) > max(self.max_width, self.max_height): + max(min_width, min_height) > max(self.max_width, self.max_height): raise ValueError("size of image is larger than max size.") else: if min_height > self.max_height or min_width > self.max_width: @@ -139,7 +139,8 @@ def _init_atlas_list(self, image_rect_list): atlas_list = [] area = self._calculate_area(image_rect_list, self.inner_padding) - w, h = self._cal_init_size(area, min_width, min_height, self.max_width, self.max_height) + w, h = self._cal_init_size( + area, min_width, min_height, self.max_width, self.max_height) atlas_list.append(self.ATLAS_TYPE(w, h, self.max_width, self.max_height, force_square=self.force_square, border_padding=self.border_padding, @@ -147,7 +148,8 @@ def _init_atlas_list(self, image_rect_list): area = area - w * h while area > 0: - w, h = self._cal_init_size(area, 0, 0, self.max_width, self.max_height) + w, h = self._cal_init_size( + area, 0, 0, self.max_width, self.max_height) area = area - w * h atlas_list.append(self.ATLAS_TYPE(w, h, self.max_width, self.max_height, force_square=self.force_square, border_padding=self.border_padding, @@ -176,14 +178,15 @@ def pack(self, input_images, output_name, output_path="", input_base_path=None): if self.trim_mode: for image_rect in image_rects: image_rect.trim(self.trim_mode) - + if self.extrude: for image_rect in image_rects: image_rect.extrude(self.extrude) atlas_list = self._pack(image_rects) - assert "%d" in output_name or len(atlas_list) == 1, 'more than one output image, but no "%d" in output_name' + assert "%d" in output_name or len( + atlas_list) == 1, 'more than one output image, but no "%d" in output_name' for i, atlas in enumerate(atlas_list): texture_file_name = output_name if "%d" not in output_name else output_name % i @@ -195,10 +198,12 @@ 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 = 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)), + atlas_data_ext = self.atlas_ext or Utils.get_atlas_data_ext( self.atlas_format) - Utils.save_image(packed_image, os.path.join(output_path, "%s%s" % (texture_file_name, self.texture_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))) def multi_pack(self, pack_args_list): """ @@ -212,6 +217,7 @@ def multi_pack(self, pack_args_list): pool_size = multiprocessing.cpu_count() * 2 pool = multiprocessing.Pool(processes=pool_size) - pool.map(multi_pack_handler, zip([self] * len(pack_args_list), pack_args_list)) + pool.map(multi_pack_handler, zip( + [self] * len(pack_args_list), pack_args_list)) pool.close() pool.join() diff --git a/PyTexturePacker/Rect.py b/PyTexturePacker/Rect.py index 627b0cc..ed1dcc7 100644 --- a/PyTexturePacker/Rect.py +++ b/PyTexturePacker/Rect.py @@ -9,7 +9,6 @@ Rect.py ----------------------------------------------------------------------------""" - class Rect(object): """ rect type data diff --git a/PyTexturePacker/Utils.py b/PyTexturePacker/Utils.py index d91b3b7..0812992 100644 --- a/PyTexturePacker/Utils.py +++ b/PyTexturePacker/Utils.py @@ -8,6 +8,7 @@ Description: Utils.py ----------------------------------------------------------------------------""" + import sys import inspect if sys.version_info.major > 2: @@ -68,9 +69,10 @@ def get_atlas_data_ext(atlas_format): 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) + 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' + return '.txt' raise ValueError(f"Unsupported file format: {atlas_format}") @@ -91,9 +93,10 @@ def save_atlas_data(data_dict, file_path, atlas_format): 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) + 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) + return atlas_format(data_dict, file_path) raise ValueError(f"Unsupported file format: {atlas_format}") @@ -106,12 +109,12 @@ def save_csv(data_dict, file_path): :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') + 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/PyTexturePacker/__init__.py b/PyTexturePacker/__init__.py index ca00948..c92dd47 100644 --- a/PyTexturePacker/__init__.py +++ b/PyTexturePacker/__init__.py @@ -1,5 +1,5 @@ -from . import Packer -from . import ImageRect -from . import PackerInterface -from . import Rect -from . import Utils +from . import Packer #noqa +from . import ImageRect #noqa +from . import PackerInterface #noqa +from . import Rect #noqa +from . import Utils #noqa diff --git a/README.rst b/README.rst index a909250..d0c4c1b 100644 --- a/README.rst +++ b/README.rst @@ -51,7 +51,7 @@ Here comes an example of using PyTexturePacker to pack texture images from a dir # create a MaxRectsBinPacker packer = Packer.create(max_width=2048, max_height=2048, bg_color=0xffffff00) # pack texture images under directory "test_case/" and name the output images as "test_case". - # "%d" in output file name "test_case%d" is a placeholder, which is a multipack index, starting with 0. + # "%d" in output file name "test_case%d" is a placeholder, which is the atlas index, starting with 0. packer.pack("test_case/", "test_case%d") @@ -172,13 +172,12 @@ The project is released under the terms of MIT License. You may find the content .. _here: http://opensource.org/licenses/MIT - -.. |build-status| image:: https://travis-ci.org/wo1fsea/PyTexturePacker.svg?branch=master - :target: https://travis-ci.org/wo1fsea/PyTexturePacker +.. |build-status| image:: https://github.com/wo1fsea/PyTexturePacker/actions/workflows/test.yml/badge.svg?branch=master + :target: https://github.com/wo1fsea/PyTexturePacker/actions/workflows/test.yml :alt: Build status .. |docs-status| image:: https://readthedocs.org/projects/pytexturepacker/badge/?version=master :target: http://pytexturepacker.readthedocs.io/ :alt: Documentation Status .. |pypi-status| image:: https://badge.fury.io/py/PyTexturePacker.svg :target: https://pypi.org/project/pytexturepacker/ - :alt: PyPI Status + :alt: PyPI Status \ No newline at end of file diff --git a/docs/1_quick_start.rst b/docs/1_quick_start.rst index 09f5cb1..d7fb400 100644 --- a/docs/1_quick_start.rst +++ b/docs/1_quick_start.rst @@ -26,5 +26,5 @@ Here comes an example of using PyTexturePacker to pack texture images from a dir # create a MaxRectsBinPacker packer = Packer.create(max_width=2048, max_height=2048, bg_color=0xffffff00) # pack texture images under directory "test_case/" and name the output images as "test_case". - # "%d" in output file name "test_case%d" is a placeholder, which is a multipack index, starting with 0. + # "%d" in output file name "test_case%d" is a placeholder, which is the atlas index, starting with 0. packer.pack("test_case/", "test_case%d") diff --git a/main.py b/main.py index 28f98c2..f647d4a 100644 --- a/main.py +++ b/main.py @@ -16,7 +16,7 @@ def pack_test(): # create a MaxRectsPacker packer = Packer.create(max_width=2048, max_height=2048, bg_color=0xffffff00) # pack texture images under the directory "test_case/" and name the output images as "test_case". - # "%d" in output file name "test_case%d" is a placeholder, which is a multipack index, starting with 0. + # "%d" in output file name "test_case%d" is a placeholder, which is the atlas index, starting with 0. packer.pack("test_image/", "test_image%d", "")