@@ -90,11 +90,11 @@ def __sample_text(cls, sess, args_text, translation):
9090 @classmethod
9191 def __crop_white_borders (cls , image ):
9292 image_data = np .asarray (image )
93- non_empty_columns = np .where (image_data .min (axis = 0 )< 255 )[0 ]
94- non_empty_rows = np .where (image_data .min (axis = 1 )< 255 )[0 ]
93+ grey_image_data = np .asarray (image .convert ('L' ))
94+ non_empty_columns = np .where (grey_image_data .min (axis = 0 ) < 255 )[0 ]
95+ non_empty_rows = np .where (grey_image_data .min (axis = 1 ) < 255 )[0 ]
9596 cropBox = (min (non_empty_rows ), max (non_empty_rows ), min (non_empty_columns ), max (non_empty_columns ))
96-
97- image_data_new = image_data [cropBox [0 ]:cropBox [1 ]+ 1 , cropBox [2 ]:cropBox [3 ]+ 1 ]
97+ image_data_new = image_data [cropBox [0 ]:cropBox [1 ]+ 1 , cropBox [2 ]:cropBox [3 ]+ 1 , :]
9898
9999 return Image .fromarray (image_data_new )
100100
@@ -105,7 +105,7 @@ def __join_images(cls, images):
105105 total_width = sum (widths ) - 35 * len (images )
106106 max_height = max (heights )
107107
108- compound_image = Image .new ('L ' , (total_width , max_height ))
108+ compound_image = Image .new ('RGBA ' , (total_width , max_height ))
109109
110110 x_offset = 0
111111 for im in images :
@@ -142,10 +142,13 @@ def generate(cls, text):
142142 for stroke in cls .__split_strokes (cls .__cumsum (np .array (coords ))):
143143 plt .plot (stroke [:, 0 ], - stroke [:, 1 ], color = '#080808' )
144144
145+ fig .patch .set_alpha (0 )
146+ fig .patch .set_facecolor ('none' )
147+
145148 canvas = plt .get_current_fig_manager ().canvas
146149 canvas .draw ()
147150
148- image = Image .frombytes ('RGB ' , canvas .get_width_height (), canvas .tostring_rgb ()). convert ( 'L' )
151+ image = Image .frombytes ('RGBA ' , canvas .get_width_height (), canvas .buffer_rgba () )
149152 images .append (cls .__crop_white_borders (image ))
150153
151154 plt .close ()
0 commit comments