44import functools
55import os , sys , warnings
66import layoutparser
7+ from itertools import cycle
78
89# We need to fix this ugly hack some time in the future
910_lib_path = os .path .dirname (sys .modules [layoutparser .__package__ ].__file__ )
1011_font_path = os .path .join (_lib_path , 'misc' , 'NotoSerifCJKjp-Regular.otf' )
1112
1213DEFAULT_BOX_WIDTH_RATIO = 0.005
1314DEFAULT_OUTLINE_COLOR = 'red'
15+ DEAFULT_COLOR_PALETTE = "#f6bd60-#f7ede2-#f5cac3-#84a59d-#f28482"
16+ # From https://coolors.co/f6bd60-f7ede2-f5cac3-84a59d-f28482
1417
1518DEFAULT_FONT_PATH = _font_path
1619DEFAULT_FONT_SIZE = 15
@@ -80,6 +83,9 @@ def _create_new_canvas(canvas, arrangement, text_background_color):
8083 raise ValueError (f"Invalid direction { arrangement } " )
8184
8285 return new_canvas
86+
87+ def _create_color_palette (types ):
88+ return {type :color for type , color in zip (types , cycle (DEAFULT_COLOR_PALETTE .split ('-' )))}
8389
8490def image_loader (func ):
8591 @functools .wraps (func )
@@ -97,7 +103,7 @@ def wrap(canvas, layout, *args, **kwargs):
97103@image_loader
98104def draw_box (canvas , layout ,
99105 box_width = None ,
100- color_map = {} ,
106+ color_map = None ,
101107 show_element_id = False ,
102108 id_font_size = None ,
103109 id_font_path = None ,
@@ -117,8 +123,10 @@ def draw_box(canvas, layout,
117123 * the maximum of (height, width) of the canvas.
118124 color_map (dict, optional):
119125 A map from `block.type` to the colors, e.g., `{1: 'red'}`.
120- Defaults to {}, when all the types are mapped to the
121- :const:`DEFAULT_OUTLINE_COLOR`.
126+ You can set it to `{}` to use only the
127+ :const:`DEFAULT_OUTLINE_COLOR` for the outlines.
128+ Defaults to None, when a color palette is is automatically
129+ created based on the input layout.
122130 show_element_id (bool, optional):
123131 Whether to display `block.id` on the top-left corner of
124132 the block.
@@ -152,6 +160,10 @@ def draw_box(canvas, layout,
152160 if show_element_id :
153161 font_obj = _create_font_object (id_font_size , id_font_path )
154162
163+ if color_map is None :
164+ all_types = set ([b .type for b in layout if hasattr (b , 'type' )])
165+ color_map = _create_color_palette (all_types )
166+
155167 for idx , ele in enumerate (layout ):
156168
157169 if isinstance (ele , Interval ):
0 commit comments