33from PIL import Image , ImageColor , ImageFont , ImageDraw , ImageFilter
44
55
6- def generate (text , font , text_color , font_size , orientation , space_width , fit ):
6+ def generate (text , font , text_color , font_size , orientation , space_width , character_spacing , fit ):
77 if orientation == 0 :
88 return _generate_horizontal_text (
9- text , font , text_color , font_size , space_width , fit
9+ text , font , text_color , font_size , space_width , character_spacing , fit
1010 )
1111 elif orientation == 1 :
1212 return _generate_vertical_text (
13- text , font , text_color , font_size , space_width , fit
13+ text , font , text_color , font_size , space_width , character_spacing , fit
1414 )
1515 else :
1616 raise ValueError ("Unknown orientation " + str (orientation ))
1717
1818
19- def _generate_horizontal_text (text , font , text_color , font_size , space_width , fit ):
19+ def _generate_horizontal_text (text , font , text_color , font_size , space_width , character_spacing , fit ):
2020 image_font = ImageFont .truetype (font = font , size = font_size )
21- words = text .split (" " )
22- space_width = image_font .getsize (" " )[0 ] * space_width
2321
24- words_width = [image_font .getsize (w )[0 ] for w in words ]
25- text_width = sum (words_width ) + int (space_width ) * (len (words ) - 1 )
26- text_height = max ([image_font .getsize (w )[1 ] for w in words ])
22+ space_width = int (image_font .getsize (" " )[0 ] * space_width )
23+
24+ char_widths = [
25+ image_font .getsize (c )[0 ] if c != " " else space_width for c in text
26+ ]
27+ text_width = sum (char_widths ) + character_spacing * (len (text ) - 1 )
28+ text_height = max ([image_font .getsize (c )[1 ] for c in text ])
2729
2830 txt_img = Image .new ("RGBA" , (text_width , text_height ), (0 , 0 , 0 , 0 ))
2931
@@ -38,10 +40,10 @@ def _generate_horizontal_text(text, font, text_color, font_size, space_width, fi
3840 rnd .randint (min (c1 [2 ], c2 [2 ]), max (c1 [2 ], c2 [2 ])),
3941 )
4042
41- for i , w in enumerate (words ):
43+ for i , c in enumerate (text ):
4244 txt_draw .text (
43- (sum (words_width [0 :i ]) + i * int ( space_width ) , 0 ),
44- w ,
45+ (sum (char_widths [0 :i ]) + i * character_spacing , 0 ),
46+ c ,
4547 fill = fill ,
4648 font = image_font ,
4749 )
@@ -52,7 +54,7 @@ def _generate_horizontal_text(text, font, text_color, font_size, space_width, fi
5254 return txt_img
5355
5456
55- def _generate_vertical_text (text , font , text_color , font_size , space_width , fit ):
57+ def _generate_vertical_text (text , font , text_color , font_size , space_width , character_spacing , fit ):
5658 image_font = ImageFont .truetype (font = font , size = font_size )
5759
5860 space_height = int (image_font .getsize (" " )[1 ] * space_width )
@@ -61,7 +63,7 @@ def _generate_vertical_text(text, font, text_color, font_size, space_width, fit)
6163 image_font .getsize (c )[1 ] if c != " " else space_height for c in text
6264 ]
6365 text_width = max ([image_font .getsize (c )[0 ] for c in text ])
64- text_height = sum (char_heights )
66+ text_height = sum (char_heights ) + character_spacing * len ( text )
6567
6668 txt_img = Image .new ("RGBA" , (text_width , text_height ), (0 , 0 , 0 , 0 ))
6769
@@ -77,7 +79,7 @@ def _generate_vertical_text(text, font, text_color, font_size, space_width, fit)
7779 )
7880
7981 for i , c in enumerate (text ):
80- txt_draw .text ((0 , sum (char_heights [0 :i ])), c , fill = fill , font = image_font )
82+ txt_draw .text ((0 , sum (char_heights [0 :i ]) + i * character_spacing ), c , fill = fill , font = image_font )
8183
8284 if fit :
8385 return txt_img .crop (txt_img .getbbox ())
0 commit comments