Skip to content

Commit

Permalink
Update Documentation, add new class called ImageNoiseAdder and fix so…
Browse files Browse the repository at this point in the history
…me bugs
  • Loading branch information
OmarSamirz committed Sep 7, 2024
1 parent f9add79 commit d360fcd
Show file tree
Hide file tree
Showing 35 changed files with 2,076 additions and 232 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

![PyPI - Version](https://img.shields.io/pypi/v/iftg)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/iftg)
![PyPI - Downloads](https://img.shields.io/pypi/dm/iftg)
[![Downloads](https://static.pepy.tech/badge/iftg)](https://pepy.tech/project/iftg)
[![Downloads](https://static.pepy.tech/badge/iftg/month)](https://pepy.tech/project/iftg)
[![Downloads](https://static.pepy.tech/badge/iftg/week)](https://pepy.tech/project/iftg)
![GitHub Release Date](https://img.shields.io/github/release-date/OmarSamirz/ImageFromTextGenerator)
![GitHub License](https://img.shields.io/github/license/OmarSamirz/ImageFromTextGenerator?logoColor=%230d7fc0)

Expand Down Expand Up @@ -63,6 +65,7 @@ IFTG offers a wide variety of noise effects that you can apply to your images to

- **Custom Noises:** IFTG provides a [noise template](noise_template.py) 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.

### Examples of Noise Effects
<table>
<tr>
<th>Background</th>
Expand Down Expand Up @@ -144,7 +147,7 @@ To get started with IFTG, follow these simple steps:
### 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:
Usage Example:
```python
from PIL import Image
from iftg.creators import ImageCreator
Expand Down Expand Up @@ -174,7 +177,7 @@ To get started with IFTG, follow these simple steps:
### 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:
Usage Example:
```python
from iftg.generators import ImagesGenerator
from iftg.noises import (BlurNoise, BrightnessNoise, DilateNoise)
Expand Down Expand Up @@ -213,15 +216,15 @@ To get started with IFTG, follow these simple steps:
# OR

# You can use for-loop to further modify your images or do something else
for i, (img, lbl) in enumerate(results):
for img, lbl, i in 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:
Usage Example:
```python
from iftg.generators import BatchesImagesGenerator
from iftg.noises import (ElasticNoise, ErodeNoise, FlipNoise)
Expand Down Expand Up @@ -258,7 +261,7 @@ To get started with IFTG, follow these simple steps:
### Adders
- **DirectoryNoiseAdder:** DirectoryNoiseAdder class is designed to add noises to images in a specific directory.

Example Usage:
Usage Example:
```python
from iftg.adders import DirectoryNoiseAdder
from iftg.noises import (GaussianNoise, PixelDropoutNoise, RotationNoise, ShadowNoise)
Expand Down
68 changes: 67 additions & 1 deletion docs/getting_started/tutorial.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,70 @@
# **Coming Soon**
# **Tutorial: To be continued**

## **Adders**


### **<a href='../../reference/adders/image_noise_adder' style="text-decoration: underline;">`ImageNoiseAdder`</a> Module**
<a href='../../reference/adders/image_noise_adder' style="text-decoration: underline;">`ImageNoiseAdder`</a> is a class used to apply various noise effects to an image and save the resulting noisy image to a specified directory. It extends the <a href='../../reference/adders/noise_adder', style="text-decoration: underline;">`NoiseAdder`</a> class, leveraging its functionality.

#### **How to use `ImageNoiseAdder`**
```py
from iftg.adders import ImageNoiseAdder
from iftg.noises import BlurNoise, BrightnessNoise

noise_adder = ImageNoiseAdder(img_path='path/to/image.tif',
output_path='',
noises=[BlurNoise(), BrightnessNoise()],
identifier: str = 'noisy',
)

noise_adder.transform_image()
```

### **<a href='../../reference/adders/directory_noise_adder' style="text-decoration: underline;">`DirectoryNoiseAdder`</a> Module**
The <a href='../../reference/adders/directory_noise_adder' style="text-decoration: underline;">`DirectoryNoiseAdder`</a> class is an extension of the abstract base class <a href='../../reference/adders/noise_adder', style="text-decoration: underline;">`NoiseAdder`</a>. It provides functionality for applying noise to multiple images located in a directory and saving the resulting noisy images to a specified output path. This class is ideal for batch image processing tasks where noise needs to be added to a collection of images.

#### **How to use `DirectoryNoiseAdder`**
```py

```

<br>
<br>

## **Creators**

### **<a href='../../reference/creators/image_creator' style="text-decoration: underline;">`ImageCreator`</a> Module**
The <a href='../../reference/creators/image_creator' style="text-decoration: underline;">`ImageCreator`</a> class extends the <a href="../../reference/creators/creator" style="text-decoration: underline;">`Creator`</a>
base class to provide functionality for generating images with customizable text, noise, and visual effects. This class is particularly useful for creating synthetic data by augmenting text-based images with noise or other transformations.

#### **How to use `ImageCreator`**
```py

```

<br>
<br>


## **Generators**

### **<a href='../../reference/generators/images_generator' style="text-decoration: underline;">`ImagesGenerator`</a> Module**

The <a href='../../reference/generators/images_generator' style="text-decoration: underline;">`ImagesGenerator`</a> class extends the <a href='../../reference/generators/generator' style="text-decoration: underline;">`Generator`</a> class to create a sequence of images with varying text and noise effects. It handles the generation of images based on the provided configuration and includes functionality to save both images and their corresponding text labels.

#### **How to use `ImagesGenerator`**
```py

```


### **<a href='../../reference/generators/batches_images_generator' style="text-decoration: underline;">`BatchesImagesGenerator`</a> Module**

<a href='../../reference/generators/batches_images_generator' style="text-decoration: underline;">`BatchesImagesGenerator`</a> is a class designed to generate images in batches, where each batch can have its own unique settings such as font, noise, background color, and margins. It inherits from the <a href='../../reference/generators/generator/' style="text-decoration: underline;">`Generator`</a> class and provides functionality to create and iterate over different batches of images.


#### **How to use `BatchesImagesGenerator`**
```py

```

4 changes: 3 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

![PyPI - Version](https://img.shields.io/pypi/v/iftg)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/iftg)
![PyPI - Downloads](https://img.shields.io/pypi/dm/iftg)
[![Downloads](https://static.pepy.tech/badge/iftg)](https://pepy.tech/project/iftg)
[![Downloads](https://static.pepy.tech/badge/iftg/month)](https://pepy.tech/project/iftg)
[![Downloads](https://static.pepy.tech/badge/iftg/week)](https://pepy.tech/project/iftg)
![GitHub Release Date](https://img.shields.io/github/release-date/OmarSamirz/ImageFromTextGenerator)
![GitHub License](https://img.shields.io/github/license/OmarSamirz/ImageFromTextGenerator?logoColor=%230d7fc0)

Expand Down
44 changes: 23 additions & 21 deletions docs/reference/adders/directory_noise_adder.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ The <a href='#directorynoiseadder-module' style="text-decoration: underline;">`D

## **Attributes**

- **dir_path : str**
- **dir_path : `str`**

The path to the directory that contains the images to be processed. The class will search for images with formats specified in `img_formats`.
The path to the directory that contains the images to be processed.

- **output_path : str**
- **output_path : `str`**

The path where the processed images will be saved. If left empty, the images will be saved in the same directory as the source images.
The path where the processed images will be saved.
!!! Note
If left empty, the images will be saved in the same directory as the source images.

- **noises : list[Noise]**
- **noises : `list[Noise]`**

A list of Noise objects that represent different noise types to be applied to the images. Each Noise object should adhere to the structure and behavior defined by the Noise class from the iftg.noises module.
A list of <a href='../../noises/noise/#noise-module' style="text-decoration: underline;">`Noise`</a> objects that represent different noise types to be applied to the images. Each <a href='../../noises/noise/#noise-module' style="text-decoration: underline;">`Noise`</a> object should adhere to the structure and behavior defined by the <a href='../../noises/noise/#noise-module' style="text-decoration: underline;">`Noise`</a> class from the `iftg.noises` module.

- **identifier : str**
- **identifier : `str`**

A unique identifier appended to the filenames of processed images. This is useful for distinguishing between original and processed images (e.g., an image named `example.tif` could become `example_noisy.tif`).

- **img_formats : list[str]**
- **img_formats : `list[str]`**

A list of image formats in which the processed images will be saved. These formats are typically strings like `'TIF'`, `'JPEG'`, etc.

Expand All @@ -36,14 +38,14 @@ This method applies the specified noise transformations to a given image.

- **Parameters:**

- **image : Image**
- **image : `Image`**

The image object to which noises will be applied.

- **Returns:**

- A tuple containing:
- The transformed `Image.Image` object (after applying noise),
- A `tuple` containing:
- The transformed `Image` object (after applying noise),
- The base name of the image file (without its extension),
- The image file extension (with a dot, e.g., `.tif`).

Expand All @@ -57,7 +59,7 @@ This method processes all the images in the directory by applying the specified

- **Returns:**

- A list of tuples, where each tuple contains:
- A `list[tuple]`, where each tuple contains:
- A noisy `Image.Image`,
- The image's base name `str`,
- The image's format or extension `str`.
Expand All @@ -72,7 +74,7 @@ This method processes all the images in the directory by applying the specified

- **Returns:**

- A list of tuples, where each tuple contains:
- A `list[tuple]`, where each tuple contains:
- A noisy `Image.Image`,
- The image's base name `str`,
- The image's format or extension `str`.
Expand All @@ -87,7 +89,7 @@ This method saves a noisy image to the output path.

- **Parameters:**

- **img_info : tuple[Image.Image, str, str]**
- **img_info : `tuple[Image.Image, str, str]`**

A tuple containing the noisy image, the base name of the image, and the image format.

Expand All @@ -101,9 +103,9 @@ This method triggers the entire process of transforming and saving images by app
It is abstract and should be implemented by subclasses to manage the full transformation workflow.

- **Returns:**
- None
- `None`

## **Example Usage**
## **Usage Example**

```py
from iftg.noises import GaussianNoise
Expand All @@ -125,17 +127,17 @@ dir_noise_adder = DirectoryNoiseAdder(
dir_noise_adder.transform_images()
```

In this example, a list of <a href='../../noises' style="text-decoration: underline;">`Noise`</a> objects is created (in this case, a <a href='../../noises' style="text-decoration: underline;">`Gaussian`</a> object), and the <a href='#directorynoiseadder-module'>`DirectoryNoiseAdder`</a> class is instantiated with the paths to input and output directories. It will apply the noise to all images in the directory and save the resulting noisy images.
In this example, a list of <a href='../../noises' style="text-decoration: underline;">`Noise`</a> objects is created (in this case, a <a href='../../noises/gaussian_noise/' style="text-decoration: underline;">`GaussianNoise`</a> object), and the <a href='#directorynoiseadder-module'>`DirectoryNoiseAdder`</a> class is instantiated with the paths to input and output directories. It will apply the noise to all images in the directory and save the resulting noisy images.

- **Notes:**
- **Image Formats:**
!!! Notes
1. **Image Formats:**

The class supports reading and saving images in any format listed in the <a href='#attributes' style="text-decoration: underline;">`img_formats`</a> attribute, making it flexible for various image file types.

- **Error Handling:**
2. **Error Handling:**
If the <a href='#attributes' style="text-decoration: underline;">`dir_path`</a> does not exist, the class raises a `FileNotFoundError`. Additionally, if no <a href='#attributes' style="text-decoration: underline;">`output_path`</a> is provided, the processed images will be saved in the original <a href='#attributes' style="text-decoration: underline;">`dir_path`</a>.

- **Efficiency:**
3. **Efficiency:**
The use of Python's `reduce` function allows for efficient chaining of noise transformations on each image. The class processes images in the order they are found, and can handle large directories by iterating over the images one by one.
125 changes: 125 additions & 0 deletions docs/reference/adders/image_noise_adder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# **<a href='#imagenoiseadder-module' style="text-decoration: underline;">`ImageNoiseAdder`</a> Module**

<a href='#imagenoiseadder-module' style="text-decoration: underline;">`ImageNoiseAdder`</a> is a class used to apply various noise effects to an image and save the resulting noisy image to a specified directory. It extends the <a href='../noise_adder/#noiseadder-module' style="text-decoration: underline;">`NoiseAdder`</a> class, leveraging its functionality.


## **Attributes**

- **img_path : `str`**

The file path to the input image that will have noise applied to it.

- **output_path : `str`**

The directory where the noisy image will be saved. Defaults to an empty string.
!!! Note
If left empty, the image will be saved in the same directory as the input image.



- **noises : `list[Noise]`**

A list of noise objects that will be applied to the image. These should be instances of <a href='../../noises/noise/#noise-module' style="text-decoration: underline;">`Noise`</a> or its subclasses.

- **identifier : `str`**

An identifier for the noisy image file. Defaults to 'noisy'.

## **Methods**

### **`_apply_noises()`**
```py
_apply_noises(self, image: Noise) -> tuple[Image.Image, str, str]:
```
This method applies the list of noise objects to the provided image.

- **Parameters:**
- **image : `Image`**

The image to which noises will be applied. This should be an instance of Image.

- **Returns:**
- `tuple`: A tuple containing:

- The noisy image `Image`.
- The base name of the image file (excluding extension) `str`.
- The image format (including the dot) `str`.

### **`add_noises()`**
This method opens the image from the specified file path <a href='#attributes' style="text-decoration: underline;">`img_path`</a>, applies the list of noises to it, and returns the noisy image along with its base name and format.

```py
def add_noises(self) -> tuple[Image.Image, str, str]:
```

- **Returns:**
- `tuple`: A tuple containing:
- The noisy image `Image`.
- The base name of the image file (excluding extension) `str`.
- The image format (including the dot) `str`.


### **`save_image()`**

```py
save_image(self, img_info: tuple[Image.Image, str, str]) -> None:
```
This method saves the noisy image to the specified output directory <a href='#attributes' style="text-decoration: underline;">`output_path`</a> using the file name and format provided.

- **Parameters:**

- img_info `tuple` containing:
- The noisy image `Image.Image`.
- The base name of the image file (without extension).
- The image format (e.g., `.tif`, `.png`).

- **Returns:**
- `None`

### **`transform_image()`**

```py
transform_image(self) -> None:
```
This method orchestrates the entire process of applying noises to the image and saving the result.

- **Returns:**
- `None`

## Usage Example

```py
from iftg.adders import ImageNoiseAdder
from iftg.noises import BlurNoise, GaussianNoise

# Create noise objects (assumes Noise is a valid class and instantiated correctly)
noise_list = [BlurNoise(), GaussianNoise()]

# Instantiate the ImageNoiseAdder
noise_adder = ImageNoiseAdder(
img_path='path/to/image.jpg',
output_path='path/to/output',
noises=noise_list,
identifier='noisy'
)

# Apply noise and save the noisy image
noise_adder.transform_image()
```

In this example, we demonstrate how to use the <a href='#imagenoiseadder-module' style="text-decoration: underline;">`ImageNoiseAdder`</a> class to apply noise effects to an image and save the result in a specified directory.

!!! Notes
1. **Error Handling:**

The constructor checks if the provided <a href='#attributes' style="text-decoration: underline;">`img_path`</a> exists. If it does not, a `FileNotFoundError` is raised. Ensure the path to the image is correct to avoid runtime errors.


2. **Flexible Output:**
The <a href='#attributes' style="text-decoration: underline;">`output_path`</a> allows specifying where the noisy image will be saved. If no path is provided (empty string), it will use the inputed image.

3. **Identifier and File Naming:**
The <a href='#attributes' style="text-decoration: underline;">`identifier`</a> helps distinguish the noisy images from the original ones. The saved image will include the identifier in its file name, followed by the batch or image number.

Loading

0 comments on commit d360fcd

Please sign in to comment.