33from PIL import Image , ImageDraw
44
55from system .lib import Console
6+ from system .lib .helper import get_sides , get_size
67from system .lib .images import get_format_by_pixel_type
78from system .lib .xcod import parse_info , FileInfo
89from system .localization import locale
@@ -33,27 +34,23 @@ def place_sprites(xcod_path: str, folder: str, overwrite: bool = False) -> (list
3334 for region_index in range (regions_count ):
3435 texture_id , points_count = xcod .read_uchar (), xcod .read_uchar ()
3536 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 )]
3738 mirroring , rotation = xcod .read_uchar () == 1 , xcod .read_char () * 90
3839
3940 filename = f'shape_{ shape_id } _{ region_index } .png'
4041 if filename not in files_to_overwrite :
4142 continue
4243
43- tmp_region = Image .open (
44- f'{ folder } { "/overwrite" if overwrite else "" } /{ filename } '
45- ).convert ('RGBA' ).rotate (- rotation , expand = True )
46-
4744 img_mask = Image .new ('L' , (texture_width , texture_height ), 0 )
4845 color = 255
49- ImageDraw .Draw (img_mask ).polygon (polygon , fill = color )
46+ ImageDraw .Draw (img_mask ).polygon (points , fill = color )
5047 bbox = img_mask .getbbox ()
5148
5249 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 )
5754
5855 if max_y - min_y != 0 :
5956 for _y in range (max_y - min_y ):
@@ -64,25 +61,26 @@ def place_sprites(xcod_path: str, folder: str, overwrite: bool = False) -> (list
6461 img_mask .putpixel ((min_x + _x - 1 , max_y - 1 ), color )
6562 else :
6663 img_mask .putpixel ((max_x - 1 , max_y - 1 ), color )
67- bbox = img_mask .getbbox ()
6864
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
7470
75- bbox = left , top , right , bottom
71+ width , height = get_size ( left , top , right , bottom )
7672
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
8174
75+ tmp_region = Image .open (
76+ f'{ folder } { "/overwrite" if overwrite else "" } /{ filename } '
77+ ).convert ('RGBA' )
8278 if mirroring :
8379 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 )
8482
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 ))
8684 sheets [texture_id ].paste (tmp_region , (left , top ), tmp_region )
8785 print ()
8886
0 commit comments