PaperFactory#

class augraphy.base.paperfactory.PaperFactory(texture_path='./paper_textures', generate_texture=0, generate_texture_background_type='random', generate_texture_edge_type='random', texture_enable_color='random', texture_color='random', texture_color_blend_method='overlay', blend_texture='random', blend_texture_path='./paper_textures', blend_generate_texture=0, blend_texture_background_type='random', blend_texture_edge_type='random', blend_method='ink_to_paper', p=1)[source]#

Bases: Augmentation

Replaces the starting paper image with a texture randomly chosen from a directory and resized to fit or cropped and tiled to fit.

Parameters:
  • texture_path (string, optional) – Directory location to pull paper textures from.

  • generate_texture (int, optional) – Flag to enable the generation of textures instead of reading it from directory.

  • generate_texture_background_type (string, optional) – Types of background texture for texture generation purpose. Use “random” for random choice. The current supported textures: 1. “normal” : Texture generated by multiple additions of normal distribution noise. 2. “strange” : Texture generated by an algorithm called “strange pattern”. 3. “rough_stains” : A rough stains similar texture generated by using FFT. 4. “fine_stains” : A fine stains similar texture generated by using FFT. 5. “severe_stains” : A severe stains similar texture generated by using FFT. 6. “light_stains” : A light stains texture covering the whole image. Generated using additive Median filter. 7. “random_pattern” : A random pattern texture generated by using FFT. 8. “dot_granular” : A granular texture generated using single directional gradient on dots of filled circle. 9. “light_granular” : A light granular texture generated using single directional gradient on gaussian noise. 10. “rough_granular” : A rough granular texture generated using using single directional gradient and stacking largest dots to smallest dots.

  • generate_texture_edge_type (string, optional) – Types of edge texture for texture generation purpose. Use “random” for random choice. The current supported textures: 1. “curvy_edge” : Texture with distinct curvy effect on the images edges. Generated by using FFT. 2. “broken_edge” : Texture with broken images edges effect. Generated by using FFT.

  • texture_enable_color (int or string, optional) – Flag to enable color in the texture. Use “random” for random choice.

  • texture_color (list or string, optional) – Color set of the texture. Use “random” for random color. Use “Blank” for preset blank paper colors. Use “Old” for preset old paper colors. Use nested list to define own color set. It should be in the format of [BGR1, BGR2…]. Each element in the list should be a tuple defining the color in BGR format.

  • texture_color_blend_method (string, optional) – Method to blend color into texture.

  • blend_texture (int or string, optional) – Flag to blend multiple textures. Use “random” for random choice.

  • blend_texture_path (string, optional) – Directory location to pull the paper textures for texture blending purpose.

  • blend_generate_texture (int, optional) – Flag to enable the generation of blending textures instead of reading it from directory.

  • blend_texture_background_type (string, optional) – Types of background texture for secondary blending texture. The primary background blending texture will be retrieved from “generate_texture_background_type”.

  • blend_texture_edge_type (string, optional) – Types of edge texture for secondary blending texture. The primary edge blending texture will be retrieved from “generate_texture_edge_type”.

  • blend_method (string, optional) – The method to blend multiple textures.

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

check_paper_edges(texture)[source]#

Crop image section with better texture.

Parameters:

texture (numpy array) – Texture image.

generate_random_texture(image, fblend)[source]#

Generate random texture by creating random or repeating patterns.

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

  • fblend (int) – Flag to identify primary or secondary texture of the blending.

resize(texture, shape)[source]#

Scales and zooms a given texture to fit a given shape.

Parameters:
  • texture (numpy array.3.) – Texture image.

  • shape (list or tuple) – x and y shape of scaled image.

retrieve_texture(image, fblend)[source]#

Retrieve image texture from the input texture path.

Parameters:

image (numpy array) – The input image.

Overview#

PaperFactory is an augmentation specific to paper phase of the augmnentation pipeline. It applies texture into image based on a random image chosen from the provided directory.

Example#

In this example, PaperFactory is use to apply texture extracted from an image in the chosen texture image directory into an input image.

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

# create directory for paper texture
paper_texture_dir = os.path.join(os.getcwd(), "paper_textures/")
os.makedirs(paper_texture_dir, exist_ok = True)

# create an empty image
image_texture = np.full((1000,1000), fill_value=255, dtype="uint8")

# create texture
for i in range(5):
    i += 8
    image_texture[::i,::i] = 0

# save texture image in the paper texture directory
cv2.imwrite(paper_texture_dir+"texture.png", image_texture)



ink_phase   = []
paper_phase = [PaperFactory(texture_path=paper_texture_dir, p=1)]
post_phase  = []
pipeline    = AugraphyPipeline(ink_phase, paper_phase, post_phase)

image = np.full((1200, 1200,3), 250, dtype="uint8")
cv2.putText(
    image,
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
    (80, 250),
    cv2.FONT_HERSHEY_SIMPLEX,
    1.2,
    0,
    3,
)

augmented_image = pipeline.augment(image)["output"]

Input image:

../../../_images/input2.png

Texture image:

../../../_images/texture.png

Augmented image:

../../../_images/output6.png