1+ from PIL import Image
2+
3+ from .exceptions import BlackBorderSizeError , WhiteBorderSizeError
14from .utils import (
2- open_image ,
3- get_box_coords ,
4- remove_extension ,
5+ PattStr ,
6+ add_border ,
7+ calculate_border ,
8+ check_path ,
9+ color_to_file ,
10+ create_and_open_patt ,
11+ generate_white_background ,
512 get_dir ,
613 get_name ,
7- check_path ,
8- PattStr ,
14+ open_image ,
915 square_image ,
1016)
11- from PIL import Image
12- from math import ceil
13-
14-
15- def get_marker_size (image , border_size ):
16- size = image .height + (border_size * 2 )
17- # Using the size double because the resulting marker should be a square.
18- return (size , size )
19-
20-
21- def create_empty_patt (filename ):
22- patt_name = remove_extension (filename ) + ".patt"
23- patt_file = open (patt_name , "w" )
24- patt_file .close ()
25- return patt_name
26-
2717
28- def create_and_open_patt (filename ):
29- patt_name = create_empty_patt (filename )
30- return open (patt_name , "a" )
18+ DEFAULT_BLACK_BORDER_PERCENTAGE = 20
19+ DEFAULT_WHITE_BORDER_PERCENTAGE = 3
3120
3221
33- def generate_patt (filename , output = None , string = False ):
22+ def generate_patt (filename : str , output : str = None , string : bool = False ):
3423 if filename :
3524 image = open_image (filename )
3625 # Patt default marker size is 16x16 pixels
@@ -56,8 +45,7 @@ def generate_patt(filename, output=None, string=False):
5645 raise FileNotFoundError
5746
5847
59- def generate_patt_from_image (image ):
60-
48+ def generate_patt_from_image (image : Image .Image ) -> PattStr :
6149 # Patt default marker size is 16x16 pixels
6250 image = image .resize ((16 , 16 ))
6351
@@ -76,60 +64,56 @@ def generate_patt_from_image(image):
7664 return patt .close ()
7765
7866
79- def generate_marker_from_image (image : Image .Image , border_percentage = 50 ):
80-
81- image = square_image (image )
82- border_size = ceil (image .height * (border_percentage / 100 ))
83-
84- new_image = generate_white_background (image )
85-
86- # Default color is black, setting (0, 0, 0) for clarity, as the border should be black
87- marker_size = get_marker_size (new_image , border_size )
88- marker = Image .new ("RGB" , marker_size , (0 , 0 , 0 ))
89- marker .paste (new_image , get_box_coords (new_image , border_size ))
67+ def generate_marker_from_image (
68+ original_image : Image .Image ,
69+ black_border_percentage = DEFAULT_BLACK_BORDER_PERCENTAGE ,
70+ white_border_percentage = DEFAULT_WHITE_BORDER_PERCENTAGE ,
71+ ) -> Image .Image :
72+ squared_image = square_image (original_image )
73+ try :
74+ black_border_size = calculate_border (
75+ squared_image .height , (black_border_percentage / 100 )
76+ )
77+ except ValueError as e :
78+ raise BlackBorderSizeError (str (e ))
79+
80+ # Create a white background if the image has transparency
81+ marker_middle_image = generate_white_background (squared_image )
82+
83+ # Add black border
84+ black = (0 , 0 , 0 )
85+ marker = add_border (marker_middle_image , black_border_size , black )
86+
87+ if white_border_percentage != 0 :
88+ try :
89+ white_border_size = calculate_border (
90+ squared_image .height , (white_border_percentage / 100 )
91+ )
92+ except ValueError as e :
93+ raise WhiteBorderSizeError (str (e ))
94+
95+ # Add white border
96+ white = (255 , 255 , 255 )
97+ marker = add_border (marker , white_border_size , white )
9098
9199 return marker
92100
93101
94- def patt_number_format (point ):
95- return str (point ).rjust (3 , " " )
96-
97-
98- # Prints all pixels from a split color to the patt file
99- def color_to_file (c , patt ):
100- n = 1
101- for point in list (c .getdata ()):
102- patt .write (patt_number_format (point ))
103- if n != 0 and n % 16 == 0 :
104- n = 0
105- patt .write ("\n " )
106- else :
107- patt .write (" " )
108- n += 1
109-
110-
111- def generate_white_background (image ):
112- if len (image .split ()) > 3 :
113- white_image = Image .new ("RGB" , image .size , (255 , 255 , 255 ))
114- white_image .paste (image , mask = image .split ()[3 ])
115- return white_image
116- return image
117-
118-
119- def generate_marker (filename , border_percentage = 50 , output = None ):
102+ def generate_marker (
103+ filename : str ,
104+ black_border_percentage : int = DEFAULT_BLACK_BORDER_PERCENTAGE ,
105+ output = None ,
106+ white_border_percentage : int = DEFAULT_WHITE_BORDER_PERCENTAGE ,
107+ ):
120108 if filename :
121109 image = open_image (filename )
122110 output = check_path (output ) if output else get_dir (filename )
123111 name = get_name (filename )
124112
125- border_size = ceil ( image . height * ( border_percentage / 100 ))
126-
127- new_image = generate_white_background ( image )
113+ border_marker = generate_marker_from_image (
114+ image , black_border_percentage , white_border_percentage
115+ )
128116
129- # Default color is black, setting (0, 0, 0) for clarity, as the border should be black
130- marker_size = get_marker_size (new_image , border_size )
131- marker = Image .new ("RGB" , marker_size , (0 , 0 , 0 ))
132- marker .paste (new_image , get_box_coords (new_image , border_size ))
133- marker .save (output + name + "_marker.png" , "PNG" )
117+ border_marker .save (output + name + "_marker.png" , "PNG" )
134118 else :
135119 raise FileNotFoundError
0 commit comments