12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from typing import List , Union , Dict , Any , Tuple
15
+ from typing import List , Optional , Union , Dict , Any , Tuple , Dict
16
16
import functools
17
17
import os
18
18
import sys
@@ -223,18 +223,19 @@ def draw_transparent_box(
223
223
224
224
@image_loader
225
225
def draw_box (
226
- canvas ,
227
- layout ,
228
- box_width = None ,
229
- box_alpha = 0 ,
230
- color_map = None ,
231
- show_element_id = False ,
232
- show_element_type = False ,
233
- id_font_size = None ,
234
- id_font_path = None ,
235
- id_text_color = None ,
236
- id_text_background_color = None ,
237
- id_text_background_alpha = 1 ,
226
+ canvas : Image .Image ,
227
+ layout : Layout ,
228
+ box_width : Optional [Union [List [int ], int ]] = None ,
229
+ box_alpha : Optional [Union [List [float ], float ]] = None ,
230
+ box_color : Optional [Union [List [str ], str ]] = None ,
231
+ color_map : Optional [Dict ] = None ,
232
+ show_element_id : bool = False ,
233
+ show_element_type : bool = False ,
234
+ id_font_size : Optional [int ] = None ,
235
+ id_font_path : Optional [str ] = None ,
236
+ id_text_color : Optional [str ] = None ,
237
+ id_text_background_color : Optional [str ] = None ,
238
+ id_text_background_alpha : Optional [float ] = 1 ,
238
239
):
239
240
"""Draw the layout region on the input canvas(image).
240
241
@@ -243,15 +244,29 @@ def draw_box(
243
244
The canvas to draw the layout boxes.
244
245
layout (:obj:`Layout` or :obj:`list`):
245
246
The layout of the canvas to show.
246
- box_width (:obj:`int`, optional):
247
+ box_width (:obj:`int` or :obj:`List[int]` , optional):
247
248
Set to change the width of the drawn layout box boundary.
248
249
Defaults to None, when the boundary is automatically
249
250
calculated as the the :const:`DEFAULT_BOX_WIDTH_RATIO`
250
251
* the maximum of (height, width) of the canvas.
251
- box_alpha (:obj:`float`, optional):
252
- A float range from 0 to 1. Set to change the alpha of the
253
- drawn layout box.
252
+ If box_with is a list, it will assign different widths to
253
+ the corresponding layout object, and should have the same
254
+ length as the number of blocks in `layout`.
255
+ box_alpha (:obj:`float` or :obj:`List[float]`, optional):
256
+ A float or list of floats ranging from 0 to 1. Set to change
257
+ the alpha of the drawn layout box.
254
258
Defaults to 0 - the layout box will be fully transparent.
259
+ If box_alpha is a list of floats, it will assign different
260
+ alphas to the corresponding layout object, and should have
261
+ the same length as the number of blocks in `layout`.
262
+ box_color (:obj:`str` or :obj:`List[str]`, optional):
263
+ A string or a list of strings for box colors, e.g.,
264
+ `['red', 'green', 'blue']` or `'red'`.
265
+ If box_color is a list of strings, it will assign different
266
+ colors to the corresponding layout object, and should have
267
+ the same length as the number of blocks in `layout`.
268
+ Defaults to None. When `box_color` is set, it will override the
269
+ `color_map`.
255
270
color_map (dict, optional):
256
271
A map from `block.type` to the colors, e.g., `{1: 'red'}`.
257
272
You can set it to `{}` to use only the
@@ -290,9 +305,6 @@ def draw_box(
290
305
A Image object containing the `layout` draw upon the input `canvas`.
291
306
"""
292
307
293
- assert 0 <= box_alpha <= 1 , ValueError (
294
- f"The box_alpha value { box_alpha } is not within range [0,1]."
295
- )
296
308
assert 0 <= id_text_background_alpha <= 1 , ValueError (
297
309
f"The id_text_background_alpha value { id_text_background_alpha } is not within range [0,1]."
298
310
)
@@ -302,30 +314,66 @@ def draw_box(
302
314
id_text_background_color = id_text_background_color or DEFAULT_TEXT_BACKGROUND
303
315
id_text_color = id_text_color or DEFAULT_TEXT_COLOR
304
316
305
- if box_width is None :
306
- box_width = _calculate_default_box_width (canvas )
307
-
308
317
if show_element_id or show_element_type :
309
318
font_obj = _create_font_object (id_font_size , id_font_path )
310
319
311
- if color_map is None :
312
- all_types = set ([b .type for b in layout if hasattr (b , "type" )])
313
- color_map = _create_color_palette (all_types )
320
+ if box_alpha is None :
321
+ box_alpha = [0 ]
322
+ else :
323
+ if isinstance (box_alpha , (float , int )):
324
+ box_alpha = [box_alpha ] * len (layout )
314
325
315
- for idx , ele in enumerate (layout ):
326
+ if len (box_alpha ) != len (layout ):
327
+ raise ValueError (
328
+ f"The number of alphas { len (box_alpha )} is not equal to the number of blocks { len (layout )} "
329
+ )
330
+ if not all (0 <= a <= 1 for a in box_alpha ):
331
+ raise ValueError (
332
+ f"The box_alpha value { box_alpha } is not within range [0,1]."
333
+ )
316
334
317
- if isinstance (ele , Interval ):
318
- ele = ele .put_on_canvas (canvas )
335
+ if box_width is None :
336
+ box_width = _calculate_default_box_width (canvas )
337
+ box_width = [box_width ] * len (layout )
338
+ else :
339
+ if isinstance (box_width , (float , int )):
340
+ box_width = [box_width ] * len (layout )
341
+
342
+ if len (box_width ) != len (layout ):
343
+ raise ValueError (
344
+ f"The number of widths { len (box_width )} is not equal to the number of blocks { len (layout )} "
345
+ )
319
346
320
- outline_color = (
347
+ if box_color is None :
348
+ if color_map is None :
349
+ all_types = set ([b .type for b in layout if hasattr (b , "type" )])
350
+ color_map = _create_color_palette (all_types )
351
+ box_color = [
321
352
DEFAULT_OUTLINE_COLOR
322
353
if not isinstance (ele , TextBlock )
323
354
else color_map .get (ele .type , DEFAULT_OUTLINE_COLOR )
324
- )
355
+ for ele in layout
356
+ ]
357
+ else :
358
+ if isinstance (box_color , str ):
359
+ box_color = [box_color ] * len (layout )
325
360
326
- _draw_box_outline_on_handler (draw , ele , outline_color , box_width )
361
+ if len (box_color ) != len (layout ):
362
+ raise ValueError (
363
+ f"The number of colors { len (box_color )} is not equal to the number of blocks { len (layout )} "
364
+ )
327
365
328
- _draw_transparent_box_on_handler (draw , ele , outline_color , box_alpha )
366
+ for idx , (ele , color , alpha , width ) in enumerate (
367
+ zip (layout , box_color , box_alpha , box_width )
368
+ ):
369
+
370
+ if isinstance (ele , Interval ):
371
+ ele = ele .put_on_canvas (canvas )
372
+
373
+ if width > 0 :
374
+ _draw_box_outline_on_handler (draw , ele , color , width )
375
+
376
+ _draw_transparent_box_on_handler (draw , ele , color , alpha )
329
377
330
378
if show_element_id or show_element_type :
331
379
text = ""
@@ -365,18 +413,18 @@ def draw_box(
365
413
def draw_text (
366
414
canvas ,
367
415
layout ,
368
- arrangement = "lr" ,
369
- font_size = None ,
370
- font_path = None ,
371
- text_color = None ,
372
- text_background_color = None ,
373
- text_background_alpha = 1 ,
374
- vertical_text = False ,
375
- with_box_on_text = False ,
376
- text_box_width = None ,
377
- text_box_color = None ,
378
- text_box_alpha = 0 ,
379
- with_layout = False ,
416
+ arrangement : str = "lr" ,
417
+ font_size : Optional [ int ] = None ,
418
+ font_path : Optional [ str ] = None ,
419
+ text_color : Optional [ str ] = None ,
420
+ text_background_color : Optional [ str ] = None ,
421
+ text_background_alpha : Optional [ float ] = None ,
422
+ vertical_text : bool = False ,
423
+ with_box_on_text : bool = False ,
424
+ text_box_width : Optional [ int ] = None ,
425
+ text_box_color : Optional [ str ] = None ,
426
+ text_box_alpha : Optional [ float ] = None ,
427
+ with_layout : bool = False ,
380
428
** kwargs ,
381
429
):
382
430
"""Draw the (detected) text in the `layout` according to
@@ -392,7 +440,6 @@ def draw_text(
392
440
image canvas:
393
441
* `lr` - left and right
394
442
* `ud` - up and down
395
-
396
443
Defaults to 'lr'.
397
444
font_size (:obj:`str`, optional):
398
445
Set to change the size of the font used for
@@ -435,7 +482,6 @@ def draw_text(
435
482
Set to change the color of the drawn layout box boundary.
436
483
Defaults to None, when the color is set to
437
484
:const:`DEFAULT_OUTLINE_COLOR`.
438
-
439
485
with_layout (bool, optional):
440
486
Whether to draw the layout boxes on the input (image) canvas.
441
487
Defaults to False.
@@ -447,6 +493,11 @@ def draw_text(
447
493
A Image object containing the drawn text from `layout`.
448
494
"""
449
495
496
+ if text_background_alpha is None :
497
+ text_background_alpha = 1
498
+ if text_box_alpha is None :
499
+ text_box_alpha = 0
500
+
450
501
assert 0 <= text_background_alpha <= 1 , ValueError (
451
502
f"The text_background_color value { text_background_alpha } is not within range [0,1]."
452
503
)
0 commit comments