Skip to main content

Simple image tools package. Used to convert, downscale or upscale images.

Project description

Python package PyPI package codecov Codacy Badge Downloads

imgtools_m8

Simple image tools package. Used to convert, downscale and/or upscale images.

Use deep learning and cv2 to upscale image using Xavier Weber models, (more info here).

Installation

Install from GitHub repository :

To install directly from GitHub:

$ python3 -m pip install "git+https://github.com/mano8/imgtools_m8 --upgrade"

To install from PypI :

python3 -m pip install imgtools_m8 --upgrade

How to use

This package automatically covert, downscale and/or upscale an image file or a list of images from directory defined in source_path property.
(See accepted extensions from cv2 documentation)

Example :

    >>> # Set source as an image file 
    >>> source_path = /path/to/image.png
    >>> # Or set source as a directory containing your images
    >>> source_path = /path/to/directory

Next you will need to define output configuration:

  • path: The output path
  • output_formats: The list of output formats,
    who contains optional output sizes and output image format
    (extension and compression options)

It is possible to resize images with different options:

  • fixed_width: resize image to exact width (in pixel)
  • fixed_height: resize image to exact height (in pixel)
  • fixed_size: resize image depending on first limitation reached (height or width)
  • fixed_width and fixed_height: resize image depending on first limitation reached.
    same as fixed_size but can set different height/width values.

Example :
The source file is 340px width and 216px height.

Recien Llegado @Cezar llañez

Recien llegado by @Cezar yañez

We want resized output file to exact width of 1900px and 1200px. And we need output formats as JPEG (with 80% quality) and WEBP (with 70% quality)

    >>> source_path = "./tests/dummy_dir/recien_llegado.jpg"
    >>> output_conf = {
            # Output path
            'path': /my/output/path/directory, 
            'output_formats': [
                {  # Get resized output file to exact width of 1600px
                    'fixed_width': 1900,
                    'formats': [
                        # JPEg defauts are 'quality': 95, 'progressive': 0, 'optimize': 0
                        {'ext': '.jpg', 'quality': 80, 'progressive': 1, 'optimize': 1},
                    ]
                },
                {  # Get resized output file to exact width of 800px
                    'fixed_width': 1200,
                    'formats': [
                        {'ext': '.jpg', 'quality': 80},
                        {'ext': '.webp', 'quality': 70}
                        {'ext': '.png', 'compression': 2}
                    ]
                }
            ]

        }
        >>> imgtools = ImageTools(
            source_path=source_path,
            output_conf=output_conf
        )
        >>> imgtools.run()

This will create 4 files in the output directory :

  • Two JPEG files resized as defined width (1900px and 1200px), with 80% quality, JPEG progressive and optimize features enabled
  • Two WEBP files resized as defined width (1900px and 1200px), with 70% quality
  • One PNG file resized as defined width (1900px), with PNG compression level = 2

The output file names are set as:

    >>> 'originalName'_'imageWidth'x'imageHeight'.'outputExtension'
    >>> # egg :
    >>> originalFileName_1400x1360.jpeg

One of above results is :

Recien Llegado @Cezar llañez

recien_llegado_1200x762.jpg by @Cezar yañez

In this case source file is precessed as:

  • upscale 2x (source file is now 680px/432px)
  • upscale 2x (source file is now 1360px/864px)
  • for 1200px width output downscale and save as recien_llegado_1200x762.jpg recien_llegado_1200x762.webp recien_llegado_1200x762.png
  • upscale 2x (source file is now 2720px/1728px)
  • for 1900px width output downscale and save as recien_llegado_1900x1207.jpeg an .webp

By default, the image tool use EDSR_x2.pb deep learning model, to improve quality.

To load any compatible model of your choice to upscale the images, you can define model_conf property.

The imgtools_m8 only contain EDSR (x2, x3, x4) and FSRCNN(x2, x3, x4) models.

If you want uses one of them set model_conf property as:

    >>> # Set EDSR_x4 model to upscale images
    >>> model_conf = {
        'file_name': 'EDSR_x4.pb',
    }
    >>> # Or set FSRCNN_x2 model to upscale images
    >>> model_conf = {
        'file_name': 'FSRCNN_x2.pb',
    }

In case you want uses another compatible model, you may download the desired .pb file, and set model_conf property as:

    >>> # Set TF-ESPCN_x2 model to upscale images
    >>> model_conf = {
        'path': "/path/to/your/downloaded/model/directory",
        'file_name': 'TF-ESPCN_x2.pb',
    }

Here a complete example using TF-ESPCN_x2 model to upscale images :

    >>> # Set TF-ESPCN_x2 model to upscale images
    >>> model_conf = {
        'path': "/path/to/your/downloaded/model/directory",
        'file_name': 'TF-ESPCN_x2.pb',
    }
    >>> source_path = "./tests/dummy_dir"
    >>> output_conf = {
            # Output path
            'path': /my/output/path/directory, 
            'output_formats': [
                {  # Get resized output file to exact width of 1600px
                    'fixed_width': 1900,
                    'formats': [
                        {'ext': '.jpg', 'quality': 80},
                        {'ext': '.webp', 'quality': 70}
                    ]
                },
                {  # Get resized output file to exact width of 800px
                    'fixed_width': 1200,
                    'formats': [
                        {'ext': '.jpg', 'quality': 80},
                        {'ext': '.webp', 'quality': 70}
                    ]
                }
            ]

        }
        >>> imgtools = ImageTools(
            source_path=source_path,
            output_conf=output_conf,
            model_conf=model_conf
        )
        >>> imgtools.run()

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

imgtools_m8-0.0.3.tar.gz (94.9 MB view details)

Uploaded Source

Built Distribution

imgtools_m8-0.0.3-py3-none-any.whl (94.9 MB view details)

Uploaded Python 3

File details

Details for the file imgtools_m8-0.0.3.tar.gz.

File metadata

  • Download URL: imgtools_m8-0.0.3.tar.gz
  • Upload date:
  • Size: 94.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for imgtools_m8-0.0.3.tar.gz
Algorithm Hash digest
SHA256 9007a867a62b1edce7a12db0ed07ea9183438506b4c94975de74d83118c32567
MD5 3a0ca4b7d5e5454a454f696cb6f64e97
BLAKE2b-256 be215e57a22ce903ba6bd0f249f7f966e25414c37164638ab03b2cd27e60fb1f

See more details on using hashes here.

File details

Details for the file imgtools_m8-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: imgtools_m8-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 94.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for imgtools_m8-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2de48886b794968ddb023a72dd8ab63b96f58a59592c7449b441e929f06a5ad2
MD5 24ef5add5a41965e52d3190da6b8520b
BLAKE2b-256 9c840302eddb73a09923b8edb63506a9568beedf483938444029eae99c6dcd77

See more details on using hashes here.

Supported by

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