@@ -21,18 +21,22 @@ def generate_from_tuple(cls, t):
2121 cls .generate (* t )
2222
2323 @classmethod
24- def generate (cls , index , text , font , out_dir , size , extension , skewing_angle , random_skew , blur , random_blur , background_type , distorsion_type , distorsion_orientation , is_handwritten , name_format , width , alignment , text_color , orientation , space_width ):
24+ def generate (cls , index , text , font , out_dir , size , extension , skewing_angle , random_skew , blur , random_blur , background_type , distorsion_type , distorsion_orientation , is_handwritten , name_format , width , alignment , text_color , orientation , space_width , margins , fit ):
2525 image = None
2626
27+ margin_top , margin_left , margin_bottom , margin_right = margins
28+ horizontal_margin = margin_left + margin_right
29+ vertical_margin = margin_top + margin_bottom
30+
2731 ##########################
2832 # Create picture of text #
2933 ##########################
3034 if is_handwritten :
3135 if orientation == 1 :
3236 raise ValueError ("Vertical handwritten text is unavailable" )
33- image = HandwrittenTextGenerator .generate (text , text_color )
37+ image = HandwrittenTextGenerator .generate (text , text_color , fit )
3438 else :
35- image = ComputerTextGenerator .generate (text , font , text_color , size , orientation , space_width )
39+ image = ComputerTextGenerator .generate (text , font , text_color , size , orientation , space_width , fit )
3640
3741 random_angle = random .randint (0 - skewing_angle , skewing_angle )
3842
@@ -68,16 +72,16 @@ def generate(cls, index, text, font, out_dir, size, extension, skewing_angle, ra
6872
6973 # Horizontal text
7074 if orientation == 0 :
71- new_width = int (float ( distorted_img .size [0 ] + 10 ) * (float (size ) / float (distorted_img .size [1 ] + 10 )))
72- resized_img = distorted_img .resize ((new_width , size - 10 ), Image .ANTIALIAS )
73- background_width = width if width > 0 else new_width + 10
75+ new_width = int (distorted_img .size [0 ] * (float (size - vertical_margin ) / float (distorted_img .size [1 ])))
76+ resized_img = distorted_img .resize ((new_width , size - vertical_margin ), Image .ANTIALIAS )
77+ background_width = width if width > 0 else new_width + horizontal_margin
7478 background_height = size
7579 # Vertical text
7680 elif orientation == 1 :
77- new_height = int (float (distorted_img .size [1 ] + 10 ) * (float (size ) / float (distorted_img .size [0 ] + 10 )))
78- resized_img = distorted_img .resize ((size - 10 , new_height ), Image .ANTIALIAS )
81+ new_height = int (float (distorted_img .size [1 ]) * (float (size - horizontal_margin ) / float (distorted_img .size [0 ])))
82+ resized_img = distorted_img .resize ((size - horizontal_margin , new_height ), Image .ANTIALIAS )
7983 background_width = size
80- background_height = new_height + 10
84+ background_height = new_height + vertical_margin
8185 else :
8286 raise ValueError ("Invalid orientation" )
8387
@@ -99,12 +103,12 @@ def generate(cls, index, text, font, out_dir, size, extension, skewing_angle, ra
99103
100104 new_text_width , _ = resized_img .size
101105
102- if alignment == 0 :
103- background .paste (resized_img , (5 , 5 ), resized_img )
106+ if alignment == 0 or width == - 1 :
107+ background .paste (resized_img , (margin_left , margin_top ), resized_img )
104108 elif alignment == 1 :
105- background .paste (resized_img , (int (background_width / 2 - new_text_width / 2 ), 5 ), resized_img )
109+ background .paste (resized_img , (int (background_width / 2 - new_text_width / 2 ), margin_top ), resized_img )
106110 else :
107- background .paste (resized_img , (background_width - new_text_width - 5 , 5 ), resized_img )
111+ background .paste (resized_img , (background_width - new_text_width - margin_right , margin_top ), resized_img )
108112
109113 ##################################
110114 # Apply gaussian blur #
0 commit comments