Skip to main content

IFTG (ImageFromTextGenerator) is a Python package designed to create datasets for Optical Character Recognition (OCR) models, by generating synthetic text images with various noise and augmentation techniques.

Project description

ImageFromTextGenerator

IFTG is a powerful Python package designed to create high-quality datasets for Optical Character Recognition (OCR) models. By generating synthetic text images with various noise and augmentation techniques, IFTG enables researchers and developers to build robust and accurate OCR systems.

Table of Contents

Why IFTG

IFTG is designed to simplify and accelerate the process of creating large and diverse OCR datasets. Here's why you should choose IFTG for your OCR model development:

  • Efficient Dataset Generation: Create large-scale synthetic datasets for OCR models with minimal effort.

  • Rich Noise Library: Access over 10 different noise types to simulate diverse real-world conditions.

  • Endless Noise Combinations: Combine multiple noise types to generate highly varied datasets.

  • Custom Noise Integration: Easily add custom noise types using the provided noise template, allowing for customized dataset creation.

  • Advanced Image Augmentation: Apply augmentations such as rotations, blurring, distortions, and more to both newly generated and existing datasets.

  • Flexible Text and Font Options: Generate images with customizable fonts, colors, sizes, and backgrounds.

  • Support for All Languages: Generate text images in any language, as long as the correct font is provided.

  • Automated Dataset Creation: Automate the process of generating and saving large datasets.

  • Distinctive Image Naming: Automatically rename images with distinctive names to differentiate between original and augmented versions.

  • User-Friendly API: Simple and intuitive API design for easy integration into your projects.

Noises

IFTG offers a wide variety of noise effects that you can apply to your images to create robust and diverse datasets for OCR models. With more than 10 noise types available, you have the flexibility to use static noises or introduce randomness in your noise application.

  • Static Noises: These noises apply consistent effects, making them useful when you want repeatable results across your dataset.

  • Random Noises: These noises introduce variability, allowing you to generate different effects with each image, enhancing the robustness of your models.

  • Custom Noises: IFTG provides a noise template that allows you to easily create your own custom noise effects. This feature gives you even more control over the image augmentation process, enabling you to tailor the noises to your specific needs.

Background Blur Brightness Dilate Elastic
Background Blur Brightness Dilate Elastic
Erode Flip Gaussian Pixel Dropout Rotation Shadow
Erode Flip Gaussian Pixel Dropout Rotation Shadow

Installation

To get started with IFTG, you'll need to install the package. You can do this using pip.

pip install iftg

Quick Start

To get started with IFTG, follow these simple steps:

  • Import the Required Classes: First, import the necessary classes from the IFTG package
    from iftg.generators import ImagesGenerator
    from iftg.noises import BlurNoise, BrightnessNoise
    
  • generate Images: Use the ImagesGenerator class to generate images with the desired text, font and apply noise
    text = ['Hello, World!']
        
    results = ImagesGenerator(text=text,
                            font_path='path/to/the/font',
                            noises=[BlurNoise(), BrightnessNoise()],                                    
                           )
    
  • Save Images: Finally, save the generated image to a file
    results.generate_images_with_text()
    

Usage

Creators

  • ImageCreator: The ImageCreator class is used to generate images with specified text and optional customization. It can be used to create images for testing or as inputs for other generator classes.

    Example Usage:

    from PIL import Image
    from iftg.creators import ImageCreator
    
    # Define the text
    text = 'Hello, World!'
    
    
    # Create an image with specified parameters
    image = ImageCreator.create_image(text=text,    # The text to be drawn on the image.
                                      font_path='path/to/the/font', # The file path to the font.
                                      noises=[],    # A list of noise objects to apply to the image.
                                      font_size=50, # The size of the font.
                                      font_color='black',   # The color of the text. It can be text or hexadecimal
                                      background_color='white', # The background color of the image. It can be text or hexadecimal
                                      margins=(5, 5, 5, 5), # Margins for text placement on the image (left, top, right, bottom). 
                                      dpi=(300, 300),   # The resolution of the image (dots per inch). 
                                      background_img=Image.open('path/to/background/image'),   # An optional background image to be used as a base.
                                      clear_font=True, # Whether to clear the font cache after creating the image.
                                     )
    
    
    # Save the created image to a file
    image.save('image.tif', **image.info)
    

Generators

  • ImageGenerator: ImagesGenerator class is designed to create images based on provided text and optional noise effects. It supports both generating images alone and with associated labels.

    Example Usage:

    from iftg.generators import ImagesGenerator
    from iftg.noises import (BlurNoise, BrightnessNoise, DilateNoise)
    
    texts = ['Hello, World!']
    
    results = ImagesGenerator(texts=texts,  # A list of texts to be used for image creation.
                              font_path= "path/to/the/font",    # The file path to the font used in the images.
                              noises=[BlurNoise(),  # A list of noise objects to be applied to the images.
                                      BrightnessNoise(),
                                      DilateNoise()
                                     ],    
                              font_size= 40,    # The size of the font used in the images.
                              font_color= 'black',  # The color of the text in the images.
                              background_color= 'white',    # The background color of the images.
                              margins= (5, 5, 5, 5),    # Margins for text placement on the images.
                              dpi= (300, 300),  # The DPI (dots per inch) settings for the images.
                              img_name='img',   # The base name for the output image files.
                              img_format='.tif',    # The file format for the output images.
                              img_output_path='output', # The directory where the generated images will be saved.
                              txt_name='text',  # The base name for the output text files containing the image labels.
                              txt_format='.txt',    # The file format for the output text files.
                              txt_output_path='output', # The directory where the generated text files will be saved.
                              auto_remove_font=True,    # A flag indicating whether to automatically remove the font from the cache after image generation.
                              background_image_path='', # The file path to the background image, if any.
                             )
    
    # Generate and save the images without labels
    results.generate_images()
    
    # OR
    
    # Generate and save images with labels
    results.generate_images_with_text()
    
    # OR
    
    # You can use for-loop to further modify you images or do something else
    for i, (img, lbl) in enumerate(results):
        img.save(f'img_{i}.tif', **img.info)
    
  • BatchesImagesGenerator: BatchesImagesGenerator class is designed to simplify the creation of multiple batches of images with minimal code. This class is particularly useful when you need to generate images in bulk, with different configurations for each batch.

    Example Usage:

    from iftg.generators import BatchesImagesGenerator
    from iftg.noises import (ElasticNoise, ErodeNoise, FlipNoise)
    
    # Define the texts for each batch
    texts = [['Hello, World!'], ['Hello, World!']]
    
    # Initialize the BatchesImagesGenerator with the specified parameters
    results = BatchesImagesGenerator(texts=texts,   # A list of lists of texts, where each inner list contains texts for one batch of images.
                                     font_paths=["path/to/the/font"],   # A list of font file paths, where each font corresponds to a batch of images.
                                     noises=[   # A list of lists of noise objects, where each inner list contains noises to be applied to one batch of images.
                                            [ElasticNoise(), FlipNoise()],
                                            [ErodeNoise(), FlipNoise]
                                            ],
                                     font_sizes=[40],   # A list of font sizes, where each size corresponds to a batch of images.
                                     font_colors=['black'], # A list of font colors, where each color corresponds to a batch of images.
                                     background_colors=['white'],   # A list of background colors, where each color corresponds to a batch of images.
                                     margins=[(5, 5, 5, 5)],    # A list of margin tuples (left, top, right, bottom) for text placement, where each margin corresponds to a batch of images.
                                     dpi=[(300, 300)],  # A list of DPI (dots per inch) settings, where each DPI value corresponds to a batch of images.
                                     img_names=['img'], # A list of base names for the output image files, where each name corresponds to a batch of images.
                                     img_formats=['.tif'],  # A list of file formats for the output images, where each format corresponds to a batch of images.
                                     img_output_paths=[''], # A list of directories where the generated images will be saved, where each directory corresponds to a batch of images.
                                     txt_names=['text'],    # A list of base names for the output text files containing image labels, where each name corresponds to a batch of images.
                                     txt_formats=['.txt'],  # A list of file formats for the output text files, where each format corresponds to a batch of images.
                                     txt_output_paths=[''], # A list of directories where the generated text files will be saved, where each directory corresponds to a batch of images.
                                     background_image_paths=['']    # A list of file paths to the background image, if any.
                                    )
    
    # Generate and save the images without labels
    results.generate_batches(is_with_label=True) # Set to False to generate images without labels
    

Adders

  • DirectoryNoiseAdder: DirectoryNoiseAdder class is designed to add noises to images in a specific directory.

    Example Usage:

    from iftg.adders import DirectoryNoiseAdder
    from iftg.noises import (GaussianNoise, PixelDropoutNoise, RotationNoise, ShadowNoise)
    
    # Initialize DirectoryNoiseAdder with the specified parameters
    results = DirectoryNoiseAdder(dir_path='path/to/directory', # The path to the directory containing images to be processed.
                                  output_path='path/to/output/directory',   # The path where the processed images will be saved.
                                  noises=[RotationNoise(), # A list of noise objects to be applied to the images.
                                          GaussianNoise(),
                                          PixelDropoutNoise(),
                                          ShadowNoise()
                                         ],
                                  identifier='noisy',   # A unique identifier to append to the filenames of the processed images.
                                  img_formats=['jpg', 'png'],   # A list of image formats to be considered for processing.
                                 )
    
    # Apply the specified noises and save the transformed images
    results.transform_images()
    

Planned Features

  • Support for Multiprocessing: Enhance performance by adding multiprocessing capabilities to speed up the image generation and noise application processes.
  • Addition of More Noise Effects: Expand the library of noise effects to provide even more options for dataset augmentation.
  • Support for Multiline Text: Enable the creation of images with multiline text, allowing for more complex and varied text-based datasets.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iftg-1.0.6.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iftg-1.0.6-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

Details for the file iftg-1.0.6.tar.gz.

File metadata

  • Download URL: iftg-1.0.6.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for iftg-1.0.6.tar.gz
Algorithm Hash digest
SHA256 ebea15c2b9d5b82f250fb5a76193828c1ef90b2148f9a95c02e2ecd76010bb0f
MD5 99ac0c030fa51e0539534b5fa6de853f
BLAKE2b-256 c1fe5b9a4869ee67e8ee11b8f0274e07158b22733bc3216c4bfa69792bb12b5b

See more details on using hashes here.

File details

Details for the file iftg-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: iftg-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 27.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for iftg-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0654abd30520bd832b73d63880b505f4cc644645296f6ede7a8457527227b096
MD5 f92bc7ab64a5601be391aefcef95cfbc
BLAKE2b-256 4167bdcf4b7f005ea5624c46942cde81115ef8c51bd5e0d085b45fedd29e70c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page