Dithering#

class augraphy.augmentations.dithering.Dithering(dither='random', order=(2, 5), numba_jit=1, p=1)[source]#

Bases: Augmentation

Applies Ordered or Floyd Steinberg dithering to the input image.

Parameters:
  • dither (string, optional) – Types of dithering, random, ordered, Floyd Steinberg dithering.

  • order (tuple, optional) – Pair of ints determining the range of order number for ordered dithering.

  • 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.

static apply_Floyd_Steinberg(image, ysize, xsize)[source]#

Run Floyd Steinberg dithering algorithm to the input image.

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

  • ysize (int) – Height of image.

  • xsize (int) – Width of image.

static apply_Ordered(image, ysize, xsize, order, ordered_matrix)[source]#

Run ordered dithering algorithm to the input image.

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

  • ysize (int) – Height of image.

  • xsize (int) – Width of image.

  • order (int) – Order number of ordered dithering.

  • ordered_matrix (list) – Ordered matrix for ordered dithering algorithm.

create_bayer(x, y, size, value, step, matrix=[[]])[source]#

Function to create ordered matrix.

Parameters:
  • x (int) – The x coordinate of current step.

  • y (int) – The y coordinate of current step.

  • size (int) – Size of ordered matrix.

  • value (int) – Value of current step.

  • step (int) – Current step value.

  • _matrix – The ordered matrix for ordered dithering algorithm.

dither_Floyd_Steinberg(image)[source]#

Apply Floyd Steinberg dithering to the input image.

Parameters:

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

dither_Ordered(image, order=5)[source]#

Apply ordered dithering to the input image.

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

  • order (int) – Order number of the ordered dithering.

Overview#

The Dithering augmentation applies Ordered or Floyd Steinberg dithering to the input image.

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 Dithering augmentation instance is initialized and it is set to ordered dithering effect. The order of dithering is set to 8.

Code example:

dirther_ordered = Dithering(dither="ordered",
                            order=8,
                            )

img_dither_ordered = dirther_ordered(image)
cv2.imshow("dither_ordered", img_dither_ordered)

Augmented image:

../../../_images/dither_ordered.png

Example 2#

In this example, a Dithering augmentation instance is initialized and it is set to Floyd Steinberg dithering effect.

Code example:

dirther_floyd = Dithering(dither="floyd" )

img_dither_floyd = dirther_floyd(image)
cv2.imshow("dither_floyd", img_dither_floyd)

Augmented image:

../../../_images/dither_floyd.png