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, to output_path directory.
See examples for more use case. (See accepted extensions from cv2 documentation)

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)

    >>> 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="./tests/dummy_dir/recien_llegado.jpg",
            output_path="/my/output/path/directory",
            output_formats=output_formats
        )
        >>> 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) models.

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

    >>> # Set EDSR_x4 model to upscale images
    >>> model_conf = {
        'model_name': 'edsr',
        'scale': 4,
    }
    >>> # Or set FSRCNN_x2 model to upscale images
    >>> model_conf = {
        'model_name': 'edsr',
        'scale': 2,
    }

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",
        'model_name': 'espcn',
        'scale': 2,
    }

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",
        'model_name': 'espcn',
        'scale': 2,
    }
    >>> 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="./tests/dummy_dir/recien_llegado.jpg",
            output_path="/my/output/path/directory",
            output_formats=output_formats,
            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-1.0.0.tar.gz (94.8 MB view details)

Uploaded Source

Built Distribution

imgtools_m8-1.0.0-py3-none-any.whl (94.8 MB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for imgtools_m8-1.0.0.tar.gz
Algorithm Hash digest
SHA256 77959d74d7759c5fc90d3e7725d027eea7674b011fbead2d26e2f036ab217c14
MD5 ecbbf3796507a9f0bc442ab413f59c8a
BLAKE2b-256 f969feaa57d19a05d3b6e8f1864748ef4f3492389c16078dfdb762aeaa442f9d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: imgtools_m8-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 94.8 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-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9897346b869253d98a6140a45bdb907f4b878974ddc6181ecc0b099ab48db6ef
MD5 090d89aa4269772747d9218a3815b6c4
BLAKE2b-256 ffadaee75aced07fc0166eabb950dca4ad3c780e151b34497481498f4aba1597

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