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.4.tar.gz (63.3 MB view details)

Uploaded Source

Built Distribution

imgtools_m8-0.0.4-py3-none-any.whl (63.2 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: imgtools_m8-0.0.4.tar.gz
  • Upload date:
  • Size: 63.3 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.4.tar.gz
Algorithm Hash digest
SHA256 b9e2195f1319d4a63ddd78b6379b1997207f62903c618390938d56961940a3e9
MD5 3f54769b69a59eb09bda57f692707735
BLAKE2b-256 8f7f45f9ef779d9d23bacf13b6c8cfdb48e04f02b6b77aa12956b60f0d79aa50

See more details on using hashes here.

File details

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

File metadata

  • Download URL: imgtools_m8-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 63.2 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 43802100ca674214c347022f4e4f009d9dd19d835cb71b8c7d20ae5729d574c7
MD5 a2b73369ba27f337b5acd22afcf23766
BLAKE2b-256 7f925c436e3a9a449ac646a07ac555e6caaea9f79ddfaee73251bb229d9d60a4

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