3
3
from PIL import Image , ImageDraw
4
4
5
5
from system .lib import Console
6
+ from system .lib .helper import get_sides , get_size
6
7
from system .lib .images import get_format_by_pixel_type
7
8
from system .lib .xcod import parse_info , FileInfo
8
9
from system .localization import locale
@@ -33,27 +34,23 @@ def place_sprites(xcod_path: str, folder: str, overwrite: bool = False) -> (list
33
34
for region_index in range (regions_count ):
34
35
texture_id , points_count = xcod .read_uchar (), xcod .read_uchar ()
35
36
texture_width , texture_height = sheets [texture_id ].width , sheets [texture_id ].height
36
- polygon = [(xcod .read_ushort (), xcod .read_ushort ()) for _ in range (points_count )]
37
+ points = [(xcod .read_ushort (), xcod .read_ushort ()) for _ in range (points_count )]
37
38
mirroring , rotation = xcod .read_uchar () == 1 , xcod .read_char () * 90
38
39
39
40
filename = f'shape_{ shape_id } _{ region_index } .png'
40
41
if filename not in files_to_overwrite :
41
42
continue
42
43
43
- tmp_region = Image .open (
44
- f'{ folder } { "/overwrite" if overwrite else "" } /{ filename } '
45
- ).convert ('RGBA' ).rotate (- rotation , expand = True )
46
-
47
44
img_mask = Image .new ('L' , (texture_width , texture_height ), 0 )
48
45
color = 255
49
- ImageDraw .Draw (img_mask ).polygon (polygon , fill = color )
46
+ ImageDraw .Draw (img_mask ).polygon (points , fill = color )
50
47
bbox = img_mask .getbbox ()
51
48
52
49
if not bbox :
53
- min_x = min (i [0 ] for i in polygon )
54
- min_y = min (i [1 ] for i in polygon )
55
- max_x = max (i [0 ] for i in polygon )
56
- max_y = max (i [1 ] for i in polygon )
50
+ min_x = min (i [0 ] for i in points )
51
+ min_y = min (i [1 ] for i in points )
52
+ max_x = max (i [0 ] for i in points )
53
+ max_y = max (i [1 ] for i in points )
57
54
58
55
if max_y - min_y != 0 :
59
56
for _y in range (max_y - min_y ):
@@ -64,25 +61,26 @@ def place_sprites(xcod_path: str, folder: str, overwrite: bool = False) -> (list
64
61
img_mask .putpixel ((min_x + _x - 1 , max_y - 1 ), color )
65
62
else :
66
63
img_mask .putpixel ((max_x - 1 , max_y - 1 ), color )
67
- bbox = img_mask .getbbox ()
68
64
69
- left , top , right , bottom = bbox
70
- if right - left - 1 :
71
- right - = 1
72
- if bottom - top - 1 :
73
- bottom - = 1
65
+ left , top , right , bottom = get_sides ( points )
66
+ if left == right :
67
+ right + = 1
68
+ if top == bottom :
69
+ bottom + = 1
74
70
75
- bbox = left , top , right , bottom
71
+ width , height = get_size ( left , top , right , bottom )
76
72
77
- width = right - left
78
- height = bottom - top
79
- region_size = width , height
80
- tmp_region = tmp_region .resize (region_size , Image .ANTIALIAS )
73
+ bbox = left , top , right , bottom
81
74
75
+ tmp_region = Image .open (
76
+ f'{ folder } { "/overwrite" if overwrite else "" } /{ filename } '
77
+ ).convert ('RGBA' )
82
78
if mirroring :
83
79
tmp_region = tmp_region .transpose (Image .FLIP_LEFT_RIGHT )
80
+ tmp_region = tmp_region .rotate (rotation , expand = True )
81
+ tmp_region = tmp_region .resize ((width , height ), Image .ANTIALIAS )
84
82
85
- sheets [texture_id ].paste (Image .new ('RGBA' , region_size ), (left , top ), img_mask .crop (bbox ))
83
+ sheets [texture_id ].paste (Image .new ('RGBA' , ( width , height ) ), (left , top ), img_mask .crop (bbox ))
86
84
sheets [texture_id ].paste (tmp_region , (left , top ), tmp_region )
87
85
print ()
88
86
0 commit comments