diff --git a/trdg/background_generator.py b/trdg/background_generator.py index 98eb67c78..bd3082406 100644 --- a/trdg/background_generator.py +++ b/trdg/background_generator.py @@ -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 @@ -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() @@ -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: diff --git a/trdg/computer_text_generator.py b/trdg/computer_text_generator.py index eb016e2ef..36fe0895f 100644 --- a/trdg/computer_text_generator.py +++ b/trdg/computer_text_generator.py @@ -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, diff --git a/trdg/generators/from_dict.py b/trdg/generators/from_dict.py index 1480788d6..b45d70c23 100644 --- a/trdg/generators/from_dict.py +++ b/trdg/generators/from_dict.py @@ -8,8 +8,6 @@ class GeneratorFromDict: - """Generator that uses words taken from pre-packaged dictionaries""" - def __init__( self, count: int = -1, @@ -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 diff --git a/trdg/generators/from_random.py b/trdg/generators/from_random.py index 737b09b6c..ec859d8eb 100644 --- a/trdg/generators/from_random.py +++ b/trdg/generators/from_random.py @@ -8,8 +8,6 @@ class GeneratorFromRandom: - """Generator that uses randomly generated words""" - def __init__( self, count: int = -1, @@ -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 diff --git a/trdg/generators/from_strings.py b/trdg/generators/from_strings.py index ef7d22efa..2bda68619 100644 --- a/trdg/generators/from_strings.py +++ b/trdg/generators/from_strings.py @@ -10,8 +10,6 @@ class GeneratorFromStrings: - """Generator that uses a given list of strings""" - def __init__( self, strings: List[str], @@ -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 diff --git a/trdg/generators/from_wikipedia.py b/trdg/generators/from_wikipedia.py index 540c7f193..70e24c87e 100644 --- a/trdg/generators/from_wikipedia.py +++ b/trdg/generators/from_wikipedia.py @@ -8,8 +8,6 @@ class GeneratorFromWikipedia: - """Generator that uses sentences taken from random Wikipedia articles""" - def __init__( self, count: int = -1, @@ -43,6 +41,65 @@ def __init__( image_mode: str = "RGB", output_bboxes: int = 0, ): + """Generator that uses words taken from random Wikipedia articles + + :param count: Number of samples to pre-generate, defaults to -1 + :type count: int, optional + :param minimum_length: Minimum number of words in the crop, defaults to 1 + :type minimum_length: 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 + """ self.generated_count = 0 self.count = count self.minimum_length = minimum_length