Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docstrings #284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 additions & 12 deletions trdg/background_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@


def gaussian_noise(height: int, width: int) -> Image:
"""
Create a background with Gaussian noise (to mimic paper)
"""
"""Create a background with Gaussian noise (to mimic paper)

:param height: Height of the generated image
:type height: int
:param width: Width of the generated image
:type width: int
:return: Background generated from gaussian noise
:rtype: Image
"""

# We create an all white image
image = np.ones((height, width)) * 255
Expand All @@ -22,17 +28,29 @@ def gaussian_noise(height: int, width: int) -> Image:


def plain_white(height: int, width: int) -> Image:
"""
Create a plain white background
"""
"""Create a plain white background

:param height: Height of the generated image
:type height: int
:param width: Width of the generated image
:type width: int
:return: White background
:rtype: Image
"""

return Image.new("L", (width, height), 255).convert("RGBA")


def quasicrystal(height: int, width: int) -> Image:
"""
Create a background with quasicrystal (https://en.wikipedia.org/wiki/Quasicrystal)
"""
"""Create a background with quasicrystal (https://en.wikipedia.org/wiki/Quasicrystal)

:param height: Height of the generated image
:type height: int
:param width: Width of the generated image
:type width: int
:return: Quasicrystal background
:rtype: Image
"""

image = Image.new("L", (width, height))
pixels = image.load()
Expand All @@ -56,9 +74,19 @@ def quasicrystal(height: int, width: int) -> Image:


def image(height: int, width: int, image_dir: str) -> Image:
"""
Create a background with a image
"""
"""Create a background with a image

:param height: Height of the generated image
:type height: int
:param width: Width of the generated image
:type width: int
:param image_dir: Image directory containing images to be used as background
:type image_dir: str
:raises Exception: Exception on empty image directory
:return: Image background
:rtype: Image
"""

images = os.listdir(image_dir)

if len(images) > 0:
Expand Down
28 changes: 28 additions & 0 deletions trdg/computer_text_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ def generate(
stroke_width: int = 0,
stroke_fill: str = "#282828",
) -> Tuple:
"""
:param text: _description_
:type text: str
:param font: _description_
:type font: str
:param text_color: _description_
:type text_color: str
:param font_size: _description_
:type font_size: int
:param orientation: _description_
:type orientation: int
:param space_width: _description_
:type space_width: int
:param character_spacing: _description_
:type character_spacing: int
:param fit: _description_
:type fit: bool
:param word_split: _description_
:type word_split: bool
:param stroke_width: _description_, defaults to 0
:type stroke_width: int, optional
:param stroke_fill: _description_, defaults to "#282828"
:type stroke_fill: str, optional
:raises ValueError: _description_
:return: _description_
:rtype: Tuple
"""

if orientation == 0:
return _generate_horizontal_text(
text,
Expand Down
67 changes: 65 additions & 2 deletions trdg/generators/from_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@


class GeneratorFromDict:
"""Generator that uses words taken from pre-packaged dictionaries"""

def __init__(
self,
count: int = -1,
Expand Down Expand Up @@ -46,6 +44,71 @@ def __init__(
path: str = "",
rtl: bool = False,
):
"""Generator that uses words taken from pre-packaged dictionaries

:param count: Number of samples to pre-generate, defaults to -1
:type count: int, optional
:param length: Number of words in the generated crop, defaults to 1
:type length: int, optional
:param allow_variable: Allow a variable number of words in the crop, defaults to False
:type allow_variable: bool, optional
:param fonts: List of font paths, defaults to []
:type fonts: List[str], optional
:param language: Language ISO code, defaults to "en"
:type language: str, optional
:param size: Text crop height (if horizontal) or width (if vertical), defaults to 32
:type size: int, optional
:param skewing_angle: Rotate the generated text, defaults to 0
:type skewing_angle: int, optional
:param random_skew: Rotate the generated text by a random value in [-skewing_angle, skewing_angle], defaults to False
:type random_skew: bool, optional
:param blur: Blur the generated text, defaults to 0
:type blur: int, optional
:param random_blur: Blur the generated text by a random value in [-blur, blur], defaults to False
:type random_blur: bool, optional
:param background_type: 0: Gaussian Noise, 1: Plain white, 2: Quasicrystal, 3: Image, defaults to 0
:type background_type: int, optional
:param distorsion_type: 0: None (Default), 1: Sine wave, 2: Cosine wave, 3: Random, defaults to 0
:type distorsion_type: int, optional
:param distorsion_orientation: 0: Vertical (Up and down), 1: Horizontal (Left and Right), 2: Both, defaults to 0
:type distorsion_orientation: int, optional
:param is_handwritten: Generate handwritten crops using an RNN, defaults to False
:type is_handwritten: bool, optional
:param width: Width of the resulting crop, defaults to -1
:type width: int, optional
:param alignment: 0: left, 1: center, 2: right. Only used if the width parameter is set, defaults to 1
:type alignment: int, optional
:param text_color: Text color, should be either a single hex color or a range in the ?,?, defaults to "#282828"
:type text_color: str, optional
:param orientation: Orientation of the text. 0: Horizontal, 1: Vertical, defaults to 0
:type orientation: int, optional
:param space_width: Width of the spaces between words. 2.0 means twice the normal space width, defaults to 1.0
:type space_width: float, optional
:param character_spacing: Width of the spaces between characters. 2 means two pixels, defaults to 0
:type character_spacing: int, optional
:param margins: Margins around the text when rendered. In pixels, defaults to (5, 5, 5, 5)
:type margins: Tuple[int, int, int, int], optional
:param fit: Apply a tight crop around the rendered text, defaults to False
:type fit: bool, optional
:param output_mask: Define if the generator will return masks for the text, defaults to False
:type output_mask: bool, optional
:param word_split: Split on words instead of on characters (preserves ligatures, no character spacing), defaults to False
:type word_split: bool, optional
:param image_dir: Image directory to use when background is set to image, defaults to os.path.join( "..", os.path.split(os.path.realpath(__file__))[0], "images" )
:type image_dir: str, optional
:param stroke_width: Width of the text strokes, defaults to 0
:type stroke_width: int, optional
:param stroke_fill: Color of the contour of the strokes, if stroke_width is bigger than 0, defaults to "#282828"
:type stroke_fill: str, optional
:param image_mode: Image mode to be used. RGB is default, L means 8-bit grayscale images, 1 means 1-bit binary images stored with one pixel per byte, defaults to "RGB"
:type image_mode: str, optional
:param output_bboxes: Define if the generator will return bounding boxes for the text, 1: Bounding box file, 2: Tesseract format, defaults to 0
:type output_bboxes: int, optional
:param path: Dictionary path, defaults to ""
:type path: str, optional
:param rtl: Right to left, set this to true for RTL languages, defaults to False
:type rtl: bool, optional
"""
self.count = count
self.length = length
self.allow_variable = allow_variable
Expand Down
68 changes: 66 additions & 2 deletions trdg/generators/from_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@


class GeneratorFromRandom:
"""Generator that uses randomly generated words"""

def __init__(
self,
count: int = -1,
Expand Down Expand Up @@ -47,6 +45,72 @@ def __init__(
image_mode: str = "RGB",
output_bboxes: int = 0,
):
"""Generator that creates words from random characters

:param count: Number of samples to pre-generate, defaults to -1
:type count: int, optional
:param length: Number of words in the generated crop, defaults to 1
:type length: int, optional
:param allow_variable: Allow a variable number of words in the crop, defaults to False
:type allow_variable: bool, optional
:param use_letters: Use letters in the random character pool, defaults to True
:type use_letters: bool, optional
:param use_numbers: Use numbers in the random character pool, defaults to True
:type use_numbers: bool, optional
:param use_symbols: Use symbols in the random character pool, defaults to True
:type use_symbols: bool, optional
:type fonts: List[str], optional
:param language: Language ISO code, defaults to "en"
:type language: str, optional
:param size: Text crop height (if horizontal) or width (if vertical), defaults to 32
:type size: int, optional
:param skewing_angle: Rotate the generated text, defaults to 0
:type skewing_angle: int, optional
:param random_skew: Rotate the generated text by a random value in [-skewing_angle, skewing_angle], defaults to False
:type random_skew: bool, optional
:param blur: Blur the generated text, defaults to 0
:type blur: int, optional
:param random_blur: Blur the generated text by a random value in [-blur, blur], defaults to False
:type random_blur: bool, optional
:param background_type: 0: Gaussian Noise, 1: Plain white, 2: Quasicrystal, 3: Image, defaults to 0
:type background_type: int, optional
:param distorsion_type: 0: None (Default), 1: Sine wave, 2: Cosine wave, 3: Random, defaults to 0
:type distorsion_type: int, optional
:param distorsion_orientation: 0: Vertical (Up and down), 1: Horizontal (Left and Right), 2: Both, defaults to 0
:type distorsion_orientation: int, optional
:param is_handwritten: Generate handwritten crops using an RNN, defaults to False
:type is_handwritten: bool, optional
:param width: Width of the resulting crop, defaults to -1
:type width: int, optional
:param alignment: 0: left, 1: center, 2: right. Only used if the width parameter is set, defaults to 1
:type alignment: int, optional
:param text_color: Text color, should be either a single hex color or a range in the ?,?, defaults to "#282828"
:type text_color: str, optional
:param orientation: Orientation of the text. 0: Horizontal, 1: Vertical, defaults to 0
:type orientation: int, optional
:param space_width: Width of the spaces between words. 2.0 means twice the normal space width, defaults to 1.0
:type space_width: float, optional
:param character_spacing: Width of the spaces between characters. 2 means two pixels, defaults to 0
:type character_spacing: int, optional
:param margins: Margins around the text when rendered. In pixels, defaults to (5, 5, 5, 5)
:type margins: Tuple[int, int, int, int], optional
:param fit: Apply a tight crop around the rendered text, defaults to False
:type fit: bool, optional
:param output_mask: Define if the generator will return masks for the text, defaults to False
:type output_mask: bool, optional
:param word_split: Split on words instead of on characters (preserves ligatures, no character spacing), defaults to False
:type word_split: bool, optional
:param image_dir: Image directory to use when background is set to image, defaults to os.path.join( "..", os.path.split(os.path.realpath(__file__))[0], "images" )
:type image_dir: str, optional
:param stroke_width: Width of the text strokes, defaults to 0
:type stroke_width: int, optional
:param stroke_fill: Color of the contour of the strokes, if stroke_width is bigger than 0, defaults to "#282828"
:type stroke_fill: str, optional
:param image_mode: Image mode to be used. RGB is default, L means 8-bit grayscale images, 1 means 1-bit binary images stored with one pixel per byte, defaults to "RGB"
:type image_mode: str, optional
:param output_bboxes: Define if the generator will return bounding boxes for the text, 1: Bounding box file, 2: Tesseract format, defaults to 0
:type output_bboxes: int, optional
"""
self.generated_count = 0
self.count = count
self.length = length
Expand Down
63 changes: 61 additions & 2 deletions trdg/generators/from_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@


class GeneratorFromStrings:
"""Generator that uses a given list of strings"""

def __init__(
self,
strings: List[str],
Expand Down Expand Up @@ -46,6 +44,67 @@ def __init__(
output_bboxes: int = 0,
rtl: bool = False,
):
"""Generator that uses words from a list of strings

:param strings: List of strings to use
:type strings: List[str]
:param count: Number of samples to pre-generate, defaults to -1
:type count: int, optional
:param fonts: List of font paths, defaults to []
:type fonts: List[str], optional
:param language: Language ISO code, defaults to "en"
:type language: str, optional
:param size: Text crop height (if horizontal) or width (if vertical), defaults to 32
:type size: int, optional
:param skewing_angle: Rotate the generated text, defaults to 0
:type skewing_angle: int, optional
:param random_skew: Rotate the generated text by a random value in [-skewing_angle, skewing_angle], defaults to False
:type random_skew: bool, optional
:param blur: Blur the generated text, defaults to 0
:type blur: int, optional
:param random_blur: Blur the generated text by a random value in [-blur, blur], defaults to False
:type random_blur: bool, optional
:param background_type: 0: Gaussian Noise, 1: Plain white, 2: Quasicrystal, 3: Image, defaults to 0
:type background_type: int, optional
:param distorsion_type: 0: None (Default), 1: Sine wave, 2: Cosine wave, 3: Random, defaults to 0
:type distorsion_type: int, optional
:param distorsion_orientation: 0: Vertical (Up and down), 1: Horizontal (Left and Right), 2: Both, defaults to 0
:type distorsion_orientation: int, optional
:param is_handwritten: Generate handwritten crops using an RNN, defaults to False
:type is_handwritten: bool, optional
:param width: Width of the resulting crop, defaults to -1
:type width: int, optional
:param alignment: 0: left, 1: center, 2: right. Only used if the width parameter is set, defaults to 1
:type alignment: int, optional
:param text_color: Text color, should be either a single hex color or a range in the ?,?, defaults to "#282828"
:type text_color: str, optional
:param orientation: Orientation of the text. 0: Horizontal, 1: Vertical, defaults to 0
:type orientation: int, optional
:param space_width: Width of the spaces between words. 2.0 means twice the normal space width, defaults to 1.0
:type space_width: float, optional
:param character_spacing: Width of the spaces between characters. 2 means two pixels, defaults to 0
:type character_spacing: int, optional
:param margins: Margins around the text when rendered. In pixels, defaults to (5, 5, 5, 5)
:type margins: Tuple[int, int, int, int], optional
:param fit: Apply a tight crop around the rendered text, defaults to False
:type fit: bool, optional
:param output_mask: Define if the generator will return masks for the text, defaults to False
:type output_mask: bool, optional
:param word_split: Split on words instead of on characters (preserves ligatures, no character spacing), defaults to False
:type word_split: bool, optional
:param image_dir: Image directory to use when background is set to image, defaults to os.path.join( "..", os.path.split(os.path.realpath(__file__))[0], "images" )
:type image_dir: str, optional
:param stroke_width: Width of the text strokes, defaults to 0
:type stroke_width: int, optional
:param stroke_fill: Color of the contour of the strokes, if stroke_width is bigger than 0, defaults to "#282828"
:type stroke_fill: str, optional
:param image_mode: Image mode to be used. RGB is default, L means 8-bit grayscale images, 1 means 1-bit binary images stored with one pixel per byte, defaults to "RGB"
:type image_mode: str, optional
:param output_bboxes: Define if the generator will return bounding boxes for the text, 1: Bounding box file, 2: Tesseract format, defaults to 0
:type output_bboxes: int, optional
:param rtl: Right to left, set this to true for RTL languages, defaults to False
:type rtl: bool, optional
"""
self.count = count
self.strings = strings
self.fonts = fonts
Expand Down
Loading