Layout and Text Visualization

layoutparser.visualization.draw_box(canvas: PIL.Image.Image, layout: layoutparser.elements.layout.Layout, box_width: Union[List[int], int, None] = None, box_alpha: Union[List[float], float, None] = None, box_color: Union[List[str], str, None] = None, color_map: Optional[Dict] = None, show_element_id: bool = False, show_element_type: bool = False, id_font_size: Optional[int] = None, id_font_path: Optional[str] = None, id_text_color: Optional[str] = None, id_text_background_color: Optional[str] = None, id_text_background_alpha: Optional[float] = 1)[source]

Draw the layout region on the input canvas(image).

Parameters
  • canvas (ndarray or Image) – The canvas to draw the layout boxes.

  • layout (Layout or list) – The layout of the canvas to show.

  • box_width (int or List[int], optional) – Set to change the width of the drawn layout box boundary. Defaults to None, when the boundary is automatically calculated as the the DEFAULT_BOX_WIDTH_RATIO * the maximum of (height, width) of the canvas. If box_with is a list, it will assign different widths to the corresponding layout object, and should have the same length as the number of blocks in layout.

  • box_alpha (float or List[float], optional) – A float or list of floats ranging from 0 to 1. Set to change the alpha of the drawn layout box. Defaults to 0 - the layout box will be fully transparent. If box_alpha is a list of floats, it will assign different alphas to the corresponding layout object, and should have the same length as the number of blocks in layout.

  • box_color (str or List[str], optional) – A string or a list of strings for box colors, e.g., [‘red’, ‘green’, ‘blue’] or ‘red’. If box_color is a list of strings, it will assign different colors to the corresponding layout object, and should have the same length as the number of blocks in layout. Defaults to None. When box_color is set, it will override the color_map.

  • color_map (dict, optional) – A map from block.type to the colors, e.g., {1: ‘red’}. You can set it to {} to use only the DEFAULT_OUTLINE_COLOR for the outlines. Defaults to None, when a color palette is is automatically created based on the input layout.

  • show_element_id (bool, optional) – Whether to display block.id on the top-left corner of the block. Defaults to False.

  • show_element_type (bool, optional) – Whether to display block.type on the top-left corner of the block. Defaults to False.

  • id_font_size (int, optional) – Set to change the font size used for drawing block.id. Defaults to None, when the size is set to DEFAULT_FONT_SIZE.

  • id_font_path (str, optional) – Set to change the font used for drawing block.id. Defaults to None, when the DEFAULT_FONT_OBJECT is used.

  • id_text_color (str, optional) – Set to change the text color used for drawing block.id. Defaults to None, when the color is set to DEFAULT_TEXT_COLOR.

  • id_text_background_color (str, optional) – Set to change the text region background used for drawing block.id. Defaults to None, when the color is set to DEFAULT_TEXT_BACKGROUND.

  • id_text_background_alpha (float, optional) – A float range from 0 to 1. Set to change the alpha of the drawn text. Defaults to 1 - the text box will be solid.

Returns

A Image object containing the layout draw upon the input canvas.

Return type

PIL.Image.Image

layoutparser.visualization.draw_text(canvas, layout, arrangement: str = 'lr', font_size: Optional[int] = None, font_path: Optional[str] = None, text_color: Optional[str] = None, text_background_color: Optional[str] = None, text_background_alpha: Optional[float] = None, vertical_text: bool = False, with_box_on_text: bool = False, text_box_width: Optional[int] = None, text_box_color: Optional[str] = None, text_box_alpha: Optional[float] = None, with_layout: bool = False, **kwargs)[source]

Draw the (detected) text in the layout according to their coordinates next to the input canvas (image) for better comparison.

Parameters
  • canvas (ndarray or Image) – The canvas to draw the layout boxes.

  • layout (Layout or list) – The layout of the canvas to show.

  • arrangement ({‘lr’, ‘ud’}, optional) – The arrangement of the drawn text canvas and the original image canvas: * lr - left and right * ud - up and down Defaults to ‘lr’.

  • font_size (str, optional) – Set to change the size of the font used for drawing block.text. Defaults to None, when the size is set to DEFAULT_FONT_SIZE.

  • font_path (str, optional) – Set to change the font used for drawing block.text. Defaults to None, when the DEFAULT_FONT_OBJECT is used.

  • text_color ([type], optional) – Set to change the text color used for drawing block.text. Defaults to None, when the color is set to DEFAULT_TEXT_COLOR.

  • text_background_color ([type], optional) – Set to change the text region background used for drawing block.text. Defaults to None, when the color is set to DEFAULT_TEXT_BACKGROUND.

  • text_background_alpha (float, optional) – A float range from 0 to 1. Set to change the alpha of the background of the canvas. Defaults to 1 - the text box will be solid.

  • vertical_text (bool, optional) – Whether the text in a block should be drawn vertically. Defaults to False.

  • with_box_on_text (bool, optional) – Whether to draw the layout box boundary of a text region on the text canvas. Defaults to False.

  • text_box_width (int, optional) – Set to change the width of the drawn layout box boundary. Defaults to None, when the boundary is automatically calculated as the the DEFAULT_BOX_WIDTH_RATIO * the maximum of (height, width) of the canvas.

  • text_box_alpha (float, optional) – A float range from 0 to 1. Set to change the alpha of the drawn text box. Defaults to 0 - the text box will be fully transparent.

  • text_box_color (int, optional) – Set to change the color of the drawn layout box boundary. Defaults to None, when the color is set to DEFAULT_OUTLINE_COLOR.

  • with_layout (bool, optional) – Whether to draw the layout boxes on the input (image) canvas. Defaults to False. When set to true, you can pass in the arguments in draw_box to change the style of the drawn layout boxes.

Returns

A Image object containing the drawn text from layout.

Return type

PIL.Image.Image