PageBorder#

class augraphy.augmentations.pageborder.PageBorder(page_border_width_height='random', page_border_color=(0, 0, 0), page_border_background_color=(0, 0, 0), page_border_use_cache_images=0, page_border_trim_sides=(0, 0, 0, 0), page_numbers='random', page_rotate_angle_in_order=1, page_rotation_angle_range=(-3, 3), curve_frequency=(0, 1), curve_height=(2, 4), curve_length_one_side=(50, 100), same_page_border=1, numba_jit=1, p=1)[source]#

Bases: Augmentation

Add page border effect by stacking images multiple times.

Parameters:
  • page_border_width_height (tuple or string , optional) – Pair of values determining the width and height of the page border effect. If width > 0, the effect applies in right side of the page, while if width <0, the effect applies in the left side of the page. If height > 0, the effect applies in bottom side of the page, while if height <0, the effect applies in the top side of the page. If the value is within the range of -1.0 to 1.0 and the value is float, border width will be scaled by image width, while border height will be sccaled by image height. width (int) = image width * width (float and -1.0 - 1.0); height (int) = image height * height (float and -1.0 - 1.0); Default value is “random”.

  • page_border_color (tuple, optional) – The color (BGR) of border effect.

  • page_border_background_color (tuple, optional) – The color (BGR) of border background.

  • page_border_use_cache_images (int, optional) – Flag to enable the usage of cache images in creating page border effect.

:param page_border_trim_sides: Tuple of 4 (left, top, right, bottom) determining which sides of the image to be trimmed.

This is valid only if same_page_border is false.

Parameters:
  • page_numbers (int, optional) – An integer determining the number of pages in the border.

  • page_rotation_angle_in_order (int , optional) – Flag to enable an ordered or random angle rotation.

  • page_rotation_angle_range (tuple , optional) – Pair of ints determining the angle of rotation in the border effect.

  • curve_frequency (tuple, optional) – Pair of ints determining number of curvy section in the generated page borders.

  • curve_height (tuple, optional) – Pair of ints determining height of curvy section in the generated page borders.

  • curve_length_one_side (tuple, optional) – Pair of ints determining one side length of generated curvy effect.

  • same_page_border (int, optional) – Flag to decide whether the added borders will be within the input image or not.

  • numba_jit (int, optional) – The flag to enable numba jit to speed up the processing in the augmentation.

  • p (float, optional) – The probability this Augmentation will be applied.

center_overlay(image_background, image_foreground, image_mask_background, image_mask_foreground)[source]#

Overlays foreground image into background image in the center.

Parameters:
  • image_background (numpy array) – The background image.

  • image_foreground (numpy array) – The foreground image.

  • image_mask_background (numpy array) – The mask of background image.

  • image_mask_foreground (numpy array) – The mask of foreground image.

create_page_borders(image, page_border_width, page_border_height, mask, keypoints, bounding_boxes)[source]#

Create page borders effect and apply it into input image.

Parameters:
  • image (numpy array) – The input image.

  • border_width (int) – Horizontal direction and width of the borders.

  • border_height (int) – Vertical direction and height of borders.

  • mask (numpy array (uint8)) – The mask of labels for each pixel. Mask value should be in range of 1 to 255. Value of 0 will be assigned to the filled area after the transformation.

  • keypoints (dictionary) – A dictionary of single or multiple labels where each label is a nested list of points coordinate.

  • bounding_boxes (list) – A nested list where each nested list contains box location (x1, y1, x2, y2).

random_folding(image)[source]#

Create random folding effect at the image border.

Parameters:

Image – Image to be folded.

Overview#

The Page Border augmentation stacking multiple images, creating an effect of single or multiple page borders on any side of the page. For additional input such as mask and keypoints, they are fully supported. For bounding boxes, their box size might be pruned if part of their box is within the border area.

Initially, a clean image with single line of text is created.

Code example:

# import libraries
import cv2
import numpy as np
from augraphy import *


# create a clean image with single line of text
image = np.full((500, 1500,3), 250, dtype="uint8")
cv2.putText(
    image,
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
    (80, 250),
    cv2.FONT_HERSHEY_SIMPLEX,
    1.5,
    0,
    3,
)

cv2.imshow("Input image", image)

Clean image:

../../../_images/input.png

Example 1#

In this example, a PageBorder augmentation instance is initialized and the page borders effect width and height is set to 30 (right) and -40 (top) pixels (30,-40). The page border color is set to black (0,0,0) and the background color is set to white (255,255,255). No trimming is done to any side of the added page border effect (0, 0, 0, 0). The border effect is set to not using any cache images (page_border_use_cache_images=0) and the number of pages is set to 10 (10). The rotation angle will be randomized (page_rotate_angle_in_order=0) and the rotation range is between 1 to 5 degree (1,5). The curvy frequency in the borders is set to random value between 0 and 1 (0,1), each with height of 1 to 2 pixels (1,2). The curvy one side length is set to random value in between 30 to 60 (30, 60). The page border effect is extended beyond the input page size (same_page_border=0) so some padding will be applied.

Code example:

page_border = PageBorder(page_border_width_height = (30, -40),
                         page_border_color=(0, 0, 0),
                         page_border_background_color=(255, 255, 255),
                         page_border_use_cache_images = 0,
                         page_border_trim_sides = (0, 0, 0, 0),
                         page_numbers = 10,
                         page_rotate_angle_in_order = 0,
                         page_rotation_angle_range = (1, 5),
                         curve_frequency=(0, 1),
                         curve_height=(1, 2),
                         curve_length_one_side=(30, 60),
                         same_page_border=0,
                         )
img_page_border = page_border(image)
cv2.imshow("page_border", img_page_border)

Augmented image:

../../../_images/page_border.png

Example 2#

In this example, a PageBorder augmentation will be applied to additional inputs such as mask, keypoints and bounding boxes. The PageBorder augmentation will be using the default parameters value and hence no additional parameters value will be specified.

Code example:

page_border = PageBorder()

img_page_border, mask, keypoints, bounding_boxes = page_border(image=image, mask=mask, keypoints=keypoints, bounding_boxes=bounding_boxes)

cv2.imshow("page_border", img_page_border)

Input mask:

../../../_images/input_mask.png

Input keypoints:

../../../_images/input_keypoints.png

Input bounding boxes:

../../../_images/input_bounding_boxes.png

Augmented image:

../../../_images/page_border2.png

Augmented mask:

../../../_images/page_border2_mask.png

Augmented keypoints:

../../../_images/page_border2_keypoints.png

Augmented bounding boxes:

../../../_images/page_border2_bounding_boxes.png