Skip to main content

A fast image resizer

Project description

cykooz.resizer

cykooz.resizer is package with the optimized version of image resizing based on Rust's crate fast_image_resize.

CHANGELOG

Installation

python3 -m pip install cykooz.resizer

Or with automatically installing Pillow:

python3 -m pip install cykooz.resizer[pillow]

Information

Supported pixel types and available optimisations:

Format Description SSE4.1 AVX2 Neon
U8 One u8 component per pixel (e.g. L) + + +
U8x2 Two u8 components per pixel (e.g. LA) + + +
U8x3 Three u8 components per pixel (e.g. RGB) + + +
U8x4 Four u8 components per pixel (e.g. RGBA, RGBx, CMYK) + + +
U16 One u16 components per pixel (e.g. L16) + + +
U16x2 Two u16 components per pixel (e.g. LA16) + + +
U16x3 Three u16 components per pixel (e.g. RGB16) + + +
U16x4 Four u16 components per pixel (e.g. RGBA16, RGBx16, CMYK16) + + +
I32 One i32 component per pixel - - -
F32 One f32 component per pixel - - -

Implemented resize algorithms:

  • Nearest - is nearest-neighbor interpolation, replacing every pixel with the nearest pixel in the output; for upscaling this means multiple pixels of the same color will be present.
  • Convolution with different filters:
    • box
    • bilinear
    • catmull_rom
    • mitchell
    • lanczos3
  • Super sampling - is resizing an image in two steps. The first step uses the "nearest" algorithm. The second step uses "convolution" with configurable filter.

Usage Examples

Resize Pillow's image

from PIL import Image

from cykooz.resizer import FilterType, ResizeAlg, Resizer


resizer = Resizer(ResizeAlg.convolution(FilterType.lanczos3))
dst_size = (255, 170)
dst_image = Image.new('RGBA', dst_size)

for i in range(1, 10):
    image = Image.open('nasa_%d-4928x3279.png' % i)
    resizer.resize_pil(image, dst_image)
    dst_image.save('nasa_%d-255x170.png' % i)

Resize raw image with an alpha channel

from cykooz.resizer import AlphaMulDiv, FilterType, ImageData, PixelType, ResizeAlg, Resizer

def resize_raw(width: int, height: int, pixels: bytes):
    src_image = ImageData(
        width,
        height,
        PixelType.U8x4,
        pixels,
    )
    alpha_mul_div = AlphaMulDiv()
    resizer = Resizer(ResizeAlg.convolution(FilterType.lanczos3))
    dst_image = ImageData(255, 170, PixelType.U8x4)
    alpha_mul_div.multiply_alpha_inplace(src_image)
    resizer.resize(src_image, dst_image)
    alpha_mul_div.divide_alpha_inplace(dst_image)    
    return dst_image

Change used CPU-extensions

from cykooz.resizer import FilterType, ResizeAlg, Resizer, CpuExtensions


resizer = Resizer(ResizeAlg.convolution(FilterType.lanczos3))
resizer.cpu_extensions = CpuExtensions.sse4_1
...

Benchmarks

Environment:

  • CPU: AMD Ryzen 9 5950X
  • RAM: DDR4 3800 MHz
  • Ubuntu 22.04 (linux 6.5.0)
  • Python 3.10
  • Rust 1.75.0
  • cykooz.resizer = "2.2"

Other Python libraries used to compare of resizing speed:

Resize algorithms:

  • Nearest
  • Convolution with Bilinear filter
  • Convolution with Lanczos3 filter

Resize RGBA image 4928x3279 => 852x567

Package (time in ms) nearest bilinear lanczos3
Pillow 0.88 105.27 200.80
cykooz.resizer 0.20 29.64 58.83
cykooz.resizer - sse4_1 0.20 14.83 27.87
cykooz.resizer - avx2 0.20 10.44 21.34

Resize grayscale (U8) image 4928x3279 => 852x567

  • Source image nasa-4928x3279.png has converted into grayscale image with one byte per pixel.
Package (time in ms) nearest bilinear lanczos3
Pillow U8 0.27 23.78 61.23
cykooz.resizer U8 0.17 5.29 11.44
cykooz.resizer U8 - sse4_1 0.17 2.30 5.90
cykooz.resizer U8 - avx2 0.17 1.85 4.17

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

cykooz.resizer-2.2.1.tar.gz (13.4 MB view hashes)

Uploaded Source

Built Distributions

cykooz.resizer-2.2.1-cp312-none-win_amd64.whl (611.3 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

cykooz.resizer-2.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (672.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

cykooz.resizer-2.2.1-cp312-cp312-macosx_10_12_x86_64.whl (666.5 kB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

cykooz.resizer-2.2.1-cp311-none-win_amd64.whl (610.7 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

cykooz.resizer-2.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (672.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

cykooz.resizer-2.2.1-cp311-cp311-macosx_10_12_x86_64.whl (667.2 kB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

cykooz.resizer-2.2.1-cp310-none-win_amd64.whl (610.7 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

cykooz.resizer-2.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (672.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

cykooz.resizer-2.2.1-cp310-cp310-macosx_10_12_x86_64.whl (667.2 kB view hashes)

Uploaded CPython 3.10 macOS 10.12+ x86-64

cykooz.resizer-2.2.1-cp39-none-win_amd64.whl (610.9 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

cykooz.resizer-2.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (673.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

cykooz.resizer-2.2.1-cp39-cp39-macosx_10_12_x86_64.whl (667.4 kB view hashes)

Uploaded CPython 3.9 macOS 10.12+ x86-64

cykooz.resizer-2.2.1-cp38-none-win_amd64.whl (611.1 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

cykooz.resizer-2.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (673.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

cykooz.resizer-2.2.1-cp38-cp38-macosx_10_12_x86_64.whl (667.6 kB view hashes)

Uploaded CPython 3.8 macOS 10.12+ x86-64

cykooz.resizer-2.2.1-cp37-none-win_amd64.whl (611.1 kB view hashes)

Uploaded CPython 3.7 Windows x86-64

cykooz.resizer-2.2.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (673.4 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

cykooz.resizer-2.2.1-cp37-cp37m-macosx_10_12_x86_64.whl (667.7 kB view hashes)

Uploaded CPython 3.7m macOS 10.12+ x86-64

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