Skip to content

Commit 9c8f28c

Browse files
committed
mod format code & mod README.rst
1 parent 61ebbda commit 9c8f28c

File tree

12 files changed

+75
-61
lines changed

12 files changed

+75
-61
lines changed

PyTexturePacker/GuillotinePacker/GuillotineAtlas.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def cut(self, main_rect, sub_rect):
5050
def place_image_rect(self, rect_index, image_rect):
5151
rect = self.max_rect_list[rect_index]
5252

53-
image_rect.x, image_rect.y = rect.x + self.inner_padding, rect.y + self.inner_padding
53+
image_rect.x, image_rect.y = rect.x + \
54+
self.inner_padding, rect.y + self.inner_padding
5455

5556
fake_image_rect = image_rect.clone()
5657
fake_image_rect.left -= self.inner_padding
@@ -61,6 +62,3 @@ def place_image_rect(self, rect_index, image_rect):
6162
self.max_rect_list.pop(rect_index)
6263
self.max_rect_list.extend(self.cut(rect, fake_image_rect))
6364
self.image_rect_list.append(image_rect)
64-
65-
66-

PyTexturePacker/ImageRect.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from .Rect import Rect
1515
from . import Utils
1616

17-
1817
class ImageRect(Rect):
1918
"""
2019
image rect data
@@ -89,7 +88,7 @@ def trim(self, v=1):
8988
self.width, self.height = self.image.size
9089

9190
self._trimmed = True
92-
91+
9392
def extrude(self, size=0):
9493
if size <= 0:
9594
return

PyTexturePacker/MaxRectsPacker/MaxRectsAtlas.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def __init__(self, *args, **kwargs):
3939

4040
width, height = self.size
4141
self.max_rect_list = [Rect(0 + self.border_padding,
42-
0 + self.border_padding,
43-
width - 2 * self.border_padding,
44-
height - 2 * self.border_padding)]
42+
0 + self.border_padding,
43+
width - 2 * self.border_padding,
44+
height - 2 * self.border_padding)]
4545

4646
def _is_in_max_size(self, size):
4747
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):
8383

8484
if old_size[0] != self.size[0]:
8585
new_rect = Rect(old_size[0] - self.border_padding,
86-
0 + self.border_padding,
87-
self.size[0] - old_size[0],
88-
self.size[1] - 2 * self.border_padding)
86+
0 + self.border_padding,
87+
self.size[0] - old_size[0],
88+
self.size[1] - 2 * self.border_padding)
8989
self.max_rect_list.append(new_rect)
9090

9191
if old_size[1] != self.size[1]:
9292
new_rect = Rect(0 + self.border_padding,
93-
old_size[1] - self.border_padding,
94-
self.size[0] - 2 * self.border_padding,
95-
self.size[1] - old_size[1])
93+
old_size[1] - self.border_padding,
94+
self.size[0] - 2 * self.border_padding,
95+
self.size[1] - old_size[1])
9696
self.max_rect_list.append(new_rect)
9797

98-
self.max_rect_list = list(filter(self._max_rect_list_pruning, self.max_rect_list))
98+
self.max_rect_list = list(
99+
filter(self._max_rect_list_pruning, self.max_rect_list))
99100

100101
return True
101102

@@ -183,7 +184,8 @@ def find_best_rank_with_rotate(self, image_rect):
183184
def place_image_rect(self, rect_index, image_rect):
184185
rect = self.max_rect_list[rect_index]
185186

186-
image_rect.x, image_rect.y = rect.x + self.inner_padding, rect.y + self.inner_padding
187+
image_rect.x, image_rect.y = rect.x + \
188+
self.inner_padding, rect.y + self.inner_padding
187189

188190
fake_image_rect = image_rect.clone()
189191
fake_image_rect.left -= self.inner_padding
@@ -200,7 +202,8 @@ def place_image_rect(self, rect_index, image_rect):
200202
_max_rect_list.append(rect)
201203

202204
self.max_rect_list = _new_max_rect_list
203-
self.max_rect_list = list(filter(self._max_rect_list_pruning, _new_max_rect_list))
205+
self.max_rect_list = list(
206+
filter(self._max_rect_list_pruning, _new_max_rect_list))
204207
self.max_rect_list.extend(_max_rect_list)
205208

206209
self.image_rect_list.append(image_rect)

PyTexturePacker/MaxRectsPacker/MaxRectsPacker.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def __init__(self, *args, **kwargs):
3030
def _pack(self, image_rect_list):
3131
atlas_list = self._init_atlas_list(image_rect_list)
3232

33-
image_rect_list = sorted(image_rect_list, key=lambda x: max(x.width, x.height), reverse=True)
33+
image_rect_list = sorted(image_rect_list, key=lambda x: max(
34+
x.width, x.height), reverse=True)
3435

3536
for image_rect in image_rect_list:
3637
best_atlas = -1
@@ -39,7 +40,8 @@ def _pack(self, image_rect_list):
3940
best_rotated = False
4041

4142
for i, max_rect in enumerate(atlas_list):
42-
index, rank, rotated = max_rect.find_best_rank(image_rect, self.enable_rotated)
43+
index, rank, rotated = max_rect.find_best_rank(
44+
image_rect, self.enable_rotated)
4345

4446
if rank < best_rank:
4547
best_atlas = i

PyTexturePacker/PackerInterface/AtlasInterface.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
AtlasInterface.py
1010
----------------------------------------------------------------------------"""
1111

12-
from ..Utils import ATLAS_FORMAT_PLIST#, ATLAS_FORMAT_JSON
12+
from ..Utils import ATLAS_FORMAT_PLIST # , ATLAS_FORMAT_JSON
1313

1414
MAX_RANK = 2 ** 32
1515
MAX_WIDTH = 1024 * 16
@@ -57,11 +57,13 @@ def dump_plist(self, texture_file_name="", input_base_path=None, atlas_format=AT
5757
if input_base_path is None:
5858
_, path = os.path.split(path)
5959
else:
60-
path = os.path.relpath(os.path.abspath(path), os.path.abspath(input_base_path))
60+
path = os.path.relpath(os.path.abspath(
61+
path), os.path.abspath(input_base_path))
6162

6263
if atlas_format == ATLAS_FORMAT_PLIST:
6364
frames[path] = dict(
64-
frame="{{%d,%d},{%d,%d}}" % (image_rect.x, image_rect.y, width, height),
65+
frame="{{%d,%d},{%d,%d}}" % (
66+
image_rect.x, image_rect.y, width, height),
6567
offset="{%d,%d}" % center_offset,
6668
rotated=bool(image_rect.rotated),
6769
sourceColorRect="{{%d,%d},{%d,%d}}" % (
@@ -70,13 +72,15 @@ def dump_plist(self, texture_file_name="", input_base_path=None, atlas_format=AT
7072
)
7173
else:
7274
frames[path] = dict(
73-
frame=dict(x=image_rect.x, y=image_rect.y, w=width, h=height),
75+
frame=dict(x=image_rect.x, y=image_rect.y,
76+
w=width, h=height),
7477
rotated=bool(image_rect.rotated),
7578
trimed=bool(image_rect.trimmed),
7679
spriteSourceSize=dict(
7780
x=image_rect.source_box[0], y=image_rect.source_box[1],
7881
w=image_rect.source_box[2], h=image_rect.source_box[3]),
79-
sourceSize=dict(w=image_rect.source_size[0], h=image_rect.source_size[1])
82+
sourceSize=dict(
83+
w=image_rect.source_size[0], h=image_rect.source_size[1])
8084
)
8185

8286
plist_data["frames"] = frames
@@ -105,6 +109,7 @@ def dump_image(self, bg_color=0xffffffff):
105109
image = image_rect.image.crop()
106110
if image_rect.rotated:
107111
image = image.transpose(Image.ROTATE_270)
108-
packed_image.paste(image, (image_rect.left, image_rect.top, image_rect.right, image_rect.bottom))
112+
packed_image.paste(
113+
image, (image_rect.left, image_rect.top, image_rect.right, image_rect.bottom))
109114

110115
return packed_image

PyTexturePacker/PackerInterface/PackerInterface.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ def _calculate_area(image_rect_list, inner_padding):
7272
area = 0
7373
for image_rect in image_rect_list:
7474
area += image_rect.area + \
75-
image_rect.width * inner_padding + \
76-
image_rect.height * inner_padding + \
77-
inner_padding ** 2
75+
image_rect.width * inner_padding + \
76+
image_rect.height * inner_padding + \
77+
inner_padding ** 2
7878
return area
7979

8080
@staticmethod
@@ -131,23 +131,25 @@ def _init_atlas_list(self, image_rect_list):
131131

132132
if self.enable_rotated:
133133
if min(min_width, min_height) > min(self.max_width, self.max_height) or \
134-
max(min_width, min_height) > max(self.max_width, self.max_height):
134+
max(min_width, min_height) > max(self.max_width, self.max_height):
135135
raise ValueError("size of image is larger than max size.")
136136
else:
137137
if min_height > self.max_height or min_width > self.max_width:
138138
raise ValueError("size of image is larger than max size.")
139139

140140
atlas_list = []
141141
area = self._calculate_area(image_rect_list, self.inner_padding)
142-
w, h = self._cal_init_size(area, min_width, min_height, self.max_width, self.max_height)
142+
w, h = self._cal_init_size(
143+
area, min_width, min_height, self.max_width, self.max_height)
143144

144145
atlas_list.append(self.ATLAS_TYPE(w, h, self.max_width, self.max_height,
145146
force_square=self.force_square, border_padding=self.border_padding,
146147
shape_padding=self.shape_padding, inner_padding=self.inner_padding))
147148

148149
area = area - w * h
149150
while area > 0:
150-
w, h = self._cal_init_size(area, 0, 0, self.max_width, self.max_height)
151+
w, h = self._cal_init_size(
152+
area, 0, 0, self.max_width, self.max_height)
151153
area = area - w * h
152154
atlas_list.append(self.ATLAS_TYPE(w, h, self.max_width, self.max_height,
153155
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):
176178
if self.trim_mode:
177179
for image_rect in image_rects:
178180
image_rect.trim(self.trim_mode)
179-
181+
180182
if self.extrude:
181183
for image_rect in image_rects:
182184
image_rect.extrude(self.extrude)
183185

184186
atlas_list = self._pack(image_rects)
185187

186-
assert "%d" in output_name or len(atlas_list) == 1, 'more than one output image, but no "%d" in output_name'
188+
assert "%d" in output_name or len(
189+
atlas_list) == 1, 'more than one output image, but no "%d" in output_name'
187190

188191
for i, atlas in enumerate(atlas_list):
189192
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):
195198
if self.reduce_border_artifacts:
196199
packed_image = Utils.alpha_bleeding(packed_image)
197200

198-
atlas_data_ext = self.atlas_ext or Utils.get_atlas_data_ext(self.atlas_format)
199-
Utils.save_atlas_data(packed_plist, os.path.join(output_path, "%s%s" % (texture_file_name, atlas_data_ext)),
201+
atlas_data_ext = self.atlas_ext or Utils.get_atlas_data_ext(
200202
self.atlas_format)
201-
Utils.save_image(packed_image, os.path.join(output_path, "%s%s" % (texture_file_name, self.texture_format)))
203+
Utils.save_atlas_data(packed_plist, os.path.join(output_path, "%s%s" % (texture_file_name, atlas_data_ext)),
204+
self.atlas_format)
205+
Utils.save_image(packed_image, os.path.join(
206+
output_path, "%s%s" % (texture_file_name, self.texture_format)))
202207

203208
def multi_pack(self, pack_args_list):
204209
"""
@@ -212,6 +217,7 @@ def multi_pack(self, pack_args_list):
212217
pool_size = multiprocessing.cpu_count() * 2
213218
pool = multiprocessing.Pool(processes=pool_size)
214219

215-
pool.map(multi_pack_handler, zip([self] * len(pack_args_list), pack_args_list))
220+
pool.map(multi_pack_handler, zip(
221+
[self] * len(pack_args_list), pack_args_list))
216222
pool.close()
217223
pool.join()

PyTexturePacker/Rect.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
Rect.py
1010
----------------------------------------------------------------------------"""
1111

12-
1312
class Rect(object):
1413
"""
1514
rect type data

PyTexturePacker/Utils.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Description:
99
Utils.py
1010
----------------------------------------------------------------------------"""
11+
1112
import sys
1213
import inspect
1314
if sys.version_info.major > 2:
@@ -68,9 +69,10 @@ def get_atlas_data_ext(atlas_format):
6869
return '.csv'
6970
elif callable(atlas_format):
7071
parameters = inspect.signature(atlas_format).parameters
71-
required_args = sum(1 for param in parameters.values() if param.default is param.empty)
72+
required_args = sum(1 for param in parameters.values()
73+
if param.default is param.empty)
7274
if len(parameters) >= 2 and required_args <= 2:
73-
return '.txt'
75+
return '.txt'
7476

7577
raise ValueError(f"Unsupported file format: {atlas_format}")
7678

@@ -91,9 +93,10 @@ def save_atlas_data(data_dict, file_path, atlas_format):
9193
return save_csv(data_dict, file_path)
9294
elif callable(atlas_format):
9395
parameters = inspect.signature(atlas_format).parameters
94-
required_args = sum(1 for param in parameters.values() if param.default is param.empty)
96+
required_args = sum(1 for param in parameters.values()
97+
if param.default is param.empty)
9598
if len(parameters) >= 2 and required_args <= 2:
96-
return atlas_format(data_dict, file_path)
99+
return atlas_format(data_dict, file_path)
97100

98101
raise ValueError(f"Unsupported file format: {atlas_format}")
99102

@@ -106,12 +109,12 @@ def save_csv(data_dict, file_path):
106109
:return:
107110
"""
108111
with open(file_path, 'w') as fp:
109-
for name, data in data_dict['frames'].items():
110-
frame = data['frame']
111-
source = data['spriteSourceSize']
112-
fp.write(f'{name},{frame["x"]},{frame["y"]},{frame["w"]},{frame["h"]},'
113-
f'{source["x"]},{source["y"]},{source["w"]},{source["h"]},'
114-
f'{data["rotated"]},{data["trimed"]}\n')
112+
for name, data in data_dict['frames'].items():
113+
frame = data['frame']
114+
source = data['spriteSourceSize']
115+
fp.write(f'{name},{frame["x"]},{frame["y"]},{frame["w"]},{frame["h"]},'
116+
f'{source["x"]},{source["y"]},{source["w"]},{source["h"]},'
117+
f'{data["rotated"]},{data["trimed"]}\n')
115118

116119

117120
def save_json(data_dict, file_path):

PyTexturePacker/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from . import Packer
2-
from . import ImageRect
3-
from . import PackerInterface
4-
from . import Rect
5-
from . import Utils
1+
from . import Packer #noqa
2+
from . import ImageRect #noqa
3+
from . import PackerInterface #noqa
4+
from . import Rect #noqa
5+
from . import Utils #noqa

README.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Here comes an example of using PyTexturePacker to pack texture images from a dir
5151
# create a MaxRectsBinPacker
5252
packer = Packer.create(max_width=2048, max_height=2048, bg_color=0xffffff00)
5353
# pack texture images under directory "test_case/" and name the output images as "test_case".
54-
# "%d" in output file name "test_case%d" is a placeholder, which is a multipack index, starting with 0.
54+
# "%d" in output file name "test_case%d" is a placeholder, which is the atlas index, starting with 0.
5555
packer.pack("test_case/", "test_case%d")
5656
5757
@@ -172,13 +172,12 @@ The project is released under the terms of MIT License. You may find the content
172172
.. _here: http://opensource.org/licenses/MIT
173173

174174

175-
176-
.. |build-status| image:: https://travis-ci.org/wo1fsea/PyTexturePacker.svg?branch=master
177-
:target: https://travis-ci.org/wo1fsea/PyTexturePacker
175+
.. |build-status| image:: https://github.com/wo1fsea/PyTexturePacker/actions/workflows/test.yml/badge.svg?branch=master
176+
:target: https://github.com/wo1fsea/PyTexturePacker/actions/workflows/test.yml
178177
:alt: Build status
179178
.. |docs-status| image:: https://readthedocs.org/projects/pytexturepacker/badge/?version=master
180179
:target: http://pytexturepacker.readthedocs.io/
181180
:alt: Documentation Status
182181
.. |pypi-status| image:: https://badge.fury.io/py/PyTexturePacker.svg
183182
:target: https://pypi.org/project/pytexturepacker/
184-
:alt: PyPI Status
183+
:alt: PyPI Status

0 commit comments

Comments
 (0)