Git Product home page Git Product logo

image_resizer's Introduction

image_resizer

This python script is meant to be user to resize large image datasets to use them in machine learning models. It uses the PIL library to resize the images and the multiprocessing library to speed up the process.

Usage

The main usage of the script is to resize bunch of images from input directory tree to output directory tree. The script will keep the same directory structure in the output directory as in the input directory.

For example, this command will resize all images to fit into 1024x1024 px from the input directory and save them in the output directory with the same directory structure:

python3 image_resizer.py -i /path/to/input_dir -o /path/to/output_dir -s 1024

The script will create the output directory if it does not exist.

Options

Full usage information can be found by running the script with the -h flag:

python3 image_resizer.py -h

This will print the following help message:

usage: image_resizer.py [-h] -i INPUT -o OUTPUT [-s SIZE] [-r RESIZE_MODE] [-f FORMAT] [-n NUM_PROCESSES] [-m] [-M] [-v]

Resize images in a directory tree.

options:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        Input directory containing images.
  -o OUTPUT, --output OUTPUT
                        Output directory to store resized images.
  -s SIZE, --size SIZE  Size to resize the images. Default is 512.
  -r RESIZE_MODE, --resize-mode RESIZE_MODE
                        Resize mode for images (thumbnail, cover or crop). Default is thumbnail.
  -f FORMAT, --format FORMAT
                        Output format for resized images (same, png, jpg, gif, tiff, bmp and webp). Default is same.
  -n NUM_PROCESSES, --num-processes NUM_PROCESSES
                        Number of processes to use for resizing. Default is number of available CPU cores.
  -m, --add-mirror      Add a mirrored version of each image.
  -M, --mirror-only     Produce only a mirrored version of each image.
  -v, --verbose         Verbose output.

Following options are available to customize the resizing process:

Option Description
-h, --help Show this help message and exit. The script will print the help message and exit.
-i, --input Input directory containing images. The script finds all JPEG and PNG images for processing.
-o, --output Output directory to store resized images. The script will keep the same directory structure in the output directory as in the input directory.
-s, --size Size to resize the images. Default is 512. The default value is chosen to be use with stable diffusion 1.5. For stable diffusion XL use size of 1024.
-r, --resize-mode Resize mode for images (thumbnail, cover or crop). Default is thumbnail. The resize modes are covering different use cases.
  • thumbnail wants to resize original image to fit in sizexsize square, this means the smaller side of the output image will be smaller then size.
  • cover wants to cover sizexsize square, the bigger side of the image will be bigger then the size.
  • crop will act as cover and crop the output image to sizexsize square.
-f, --format Output format for resized images (same, png, jpg, gif, tiff, bmp and webp). Default is same. The script can additionally produce output in specific image format.
  • same will save output files in original image format.
  • png will convert images to PNG format.
  • jpg will generate JPEG output files.
  • gif will generate GIF output files.
  • tiff will generate TIFF output files.
  • bmp will generate BMP output files.
  • webp will generate WEBP output files.
-n, --num-processes Number of processes to use for resizing. Default is number of available CPU cores. The script will use multiprocessing to speed up the resizing process.
-m, --add-mirror Add a mirrored version of each image. The script will add a mirrored version of each image to the output directory. The mirrored file will be saved with following name format <original_image>_mirror.<ext>. The idea of adding mirrored versions of the images to your training dataset is to remove biases from your input images.
-M, --mirror-only Produce only a mirrored version of each image. The script will produce only a mirrored version of each image to the output directory. This option can be used to add mirrored images to existing output directory tree.
-v, --verbose Verbose output. The script will print more information about the processing. Adding more -v options will increase information you will see during directory resizing.

How You Can Contribute

We welcome your contributions to the ag-panel project! Whether it's code, resources, or financial support, your help is invaluable. Feel free to reach out directly via email at [email protected] or through my GitHub profile.

If you'd like to make a financial contribution to support the project's development, you can donate via PayPal or Ko-Fi. Your generosity is greatly appreciated.

Thank you in advance for your support, and let's make the ag-panel project even better together!

image_resizer's People

Contributors

red-scorp avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.