BookBinding#

class augraphy.augmentations.bookbinding.BookBinding(shadow_radius_range=(30, 100), curve_range_right=(50, 100), curve_range_left=(200, 300), curve_ratio_right=(0.05, 0.1), curve_ratio_left=(0.5, 0.6), mirror_range=(1.0, 1.0), binding_align='random', binding_pages=(5, 10), curling_direction=-1, backdrop_color=(0, 0, 0), enable_shadow=1, use_cache_images=1, numba_jit=1, p=1)[source]#

Bases: Augmentation

Creates a book binding effect by applying shadow and combining two bended images.

The input image will be served as the right image. If cache image is set, the left image will be selected from cache images, else it will be using the input image.

Parameters:
  • shadow_radius_range (tuple, optional) – The range of radius in pixels for the darken shadow effect.

  • curve_range_right (tuple, optional) – Tuple of ints determing the amount of curving down pixels in the right image.

  • curve_range_left (tuple, optional) – Tuple of ints determing the amount of curving down pixels in the left image.

  • curve_ratio_right (tuple, optional) – Tuple of floats determing the percentage of right image (in width) should be squeezed horizontally.

  • curve_ratio_left (tuple, optional) – Tuple of floats determing the percentage of left image (in width) should be squeezed horizontally.

  • mirror_range (Tuple, optional) – Tuple of floats to determine percentage of image to be mirrored.

  • binding_align – Flag to determine whether the pages in the binding effect should be properly aligned or not.

:type binding_align int, optional :param binding_pages: Tuple of ints determing the number of pages in the book binding effect. :type binding_pages tuple, optional :param curling_direction: The direction of page curling, -1: random, 0: up, 1: down. :type curling_direction: int, optional :param backdrop_color: The backdrop color (BGR) of the book binding effect. :type backdrop_color: tuple, optional :param enable_shadow: Flag to enable shadow on top of the book binding effect. :type enable_shadow: int, optional :param use_cache_images: Flag to enable the usage of cache images in creating book binding effect. :type use_cache_images: int, optional :param numba_jit: The flag to enable numba jit to speed up the processing in the augmentation. :type numba_jit: int, optional :param p: The probability that this Augmentation will be applied. :type p: float, optional

add_book_shadow(img, radius, angle=30)[source]#

Add shadow effect in the input image.

Parameters:
  • img (numpy.array (numpy.uint8)) – The image to apply the function.

  • radius (int) – Radius of the shadow effect.

  • angle (int) – Angle value to generate shadow effect.

static check_backdrop_color(image_output, image_mask, backdrop_color)[source]#

Function to compute mask of background

Parameters:
  • image_output (numpy.array (numpy.uint8)) – The image with BookBinding effect.

  • image_mask (numpy.array (numpy.uint8)) – The mask of background.

  • backdrop_color (tuple) – The backdrop color in BGR format.

curve_page(img, curve_value, backdrop_color, mask=None, keypoints=None, bounding_boxes=None)[source]#

Generate curvy effect in the input image.

Parameters:
  • img (numpy.array (numpy.uint8)) – The image to apply the function.

  • curve_value (int) – Pixel number of the page text should be curved.

  • backdrop_color (tuple) – The color of the filled background.

  • mask (numpy array (uint8), optional) – 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, optional) – A dictionary of single or multiple labels where each label is a nested list of points coordinate.

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

static curve_page_processing(img, img_output, curve_value, rows, cols, curve_factor)[source]#

Function to apply sin function to create wavy image.

Parameters:
  • img (numpy.array (numpy.uint8)) – The image to apply the function.

  • img_output (numpy.array (numpy.uint8)) – The output image from the function.

  • curve_value (int) – Pixel number of the page text should be curved.

  • rows (int) – Number of rows in input image.

  • cols (int) – Number of columns in input image.

  • curve_factor (float) – The curve factor determines the opening size of book binding effect

curve_processing(image, image_left, mask=None, keypoints=None, bounding_boxes=None)[source]#

Core function for curvy effect processing.

Parameters:
  • image (numpy.array (numpy.uint8)) – The right image of the book binding effect.

  • image_left (numpy.array (numpy.uint8)) – The left image of the book binding effect.

  • mask (numpy array (uint8), optional) – 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, optional) – A dictionary of single or multiple labels where each label is a nested list of points coordinate.

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

curve_transform(image, curve_ratio_range, image_direction, mask=None, keypoints=None, bounding_boxes=None)[source]#

Bend image further in horizontal direction using perspective transform.

Parameters:
  • image (numpy.array (numpy.uint8)) – The image to apply the function.

  • curve_ratio (float) – The ratio of transformation in horizontal direction..

  • image_direction (string) – Flag to identify left or right side of image.

  • mask (numpy array (uint8), optional) – 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, optional) – A dictionary of single or multiple labels where each label is a nested list of points coordinate.

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

Overview#

The BookBinding augmentation creates the effect of a page curling away from the scanner bed, towards the binding of a book or magazine. Right now it supports 2 variants of page curling direction. For additional input such as mask and keypoints, they are fully supported. For bounding boxes, only the start point or end point of the box will be affected.

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 BookBinding augmentation instance is initialized and the curling direction is set to bend up (curling_direction=0). The shadow radius range is set to 100 pixels (100,100). The amount of curving down pixels in both of the right and left images are set to 300 (300, 300). The horizontal bending ratio in both both of the right and left image are 30% (0.3, 0.3). The percentage of left page mirroring for the binding effect is set to the 100% (1, 1). The pages in the book binding effect is set to align randomly (binding_align = 0) and the number of pages is set to 10 (10, 10). The backdrop color of the effect is set to white color (255, 255, 255) and shadow effect is added (enable_shadow=1). No cache images will be used to generate the pages (use_cache_images = 0).

Code example:

book_binder_up = BookBinding(shadow_radius_range=(100, 100),
                             curve_range_right=(300, 300),
                             curve_range_left=(300, 300),
                             curve_ratio_right = (0.3, 0.3),
                             curve_ratio_left = (0.3, 0.3),
                             mirror_range=(1.0, 1.0),
                             binding_align = 0,
                             binding_pages = (10,10),
                             curling_direction=0,
                             backdrop_color=(255, 255, 255),
                             enable_shadow=1,
                             use_cache_images = 0,
                             )

img_book_binding_up = book_binder_up(image)

cv2.imshow("book_binding_up", img_book_binding_up)

Augmented image:

../../../_images/book_binding_up.png

Example 2#

In this example, a BookBinding augmentation instance is initialized and the curling direction is set to bend down (curling_direction=1). The shadow radius range is set to 100 pixels (100,100). The amount of curving down pixels in the right image is set to 50 (50, 50) while The amount of curving down pixels in the left image is set to 300 (300, 300). The horizontal bending ratio in right image is 5% (0.05, 0.05) while the horizontal bending ratio in left image is 30% (0.3, 0.3). The percentage of left page mirroring for the binding effect is set to the 50% (0.5, 0.5). The pages in the book binding effect is set to align properly (binding_align = 1) and the number of pages is set to 10 (10, 10). The backdrop color of the effect is set to white color (255, 255, 255) and no shadow effect is added (enable_shadow=0). No cache images will be used to generate the pages (use_cache_images = 0).

Code example:

book_binder_down = BookBinding(shadow_radius_range=(100, 100),
                              curve_range_right=(50, 50),
                              curve_range_left=(300, 300),
                              curve_ratio_right = (0.05, 0.05),
                              curve_ratio_left = (0.3, 0.3),
                              mirror_range=(0.50, 0.50),
                              binding_align = 1,
                              binding_pages = (10,10),
                              curling_direction=1,
                              backdrop_color=(255, 255, 255),
                              enable_shadow=0,
                              use_cache_images = 0,
                              )


img_book_binding_down = book_binder_down(image)

cv2.imshow("book_binding_down", img_book_binding_down)

Augmented image:

../../../_images/book_binding_down.png

Example 3#

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

Code example:

book_binder = BookBinding()

img_book_binding, mask, keypoints, bounding_boxes = book_binder(image=image, mask=mask, keypoints=keypoints, bounding_boxes=bounding_boxes)

cv2.imshow("book_binding", img_book_binding)

Input mask:

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

Input keypoints:

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

Input bounding boxes:

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

Augmented image:

../../../_images/book_binding3.png

Augmented mask:

../../../_images/book_binding3_mask.png

Augmented keypoints:

../../../_images/book_binding3_keypoints.png

Augmented bounding boxes:

../../../_images/book_binding3_bounding_boxes.png