Skip to main content

A fast image resizer

Project description

cykooz.resizer

cykooz.resizer is package with 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 Native Rust SSE4.1 AVX2
U8 One u8 component per pixel (e.g. L) + partial +
U8x2 Two u8 components per pixel (e.g. LA) + + +
U8x3 Three u8 components per pixel (e.g. RGB) + partial +
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 - resizing an image in two steps. First step uses the "nearest" algorithm. 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 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 5.15.0)
  • Python 3.9
  • Rust 1.62.0
  • cykooz.resizer = "2.1"

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.66 93.16 179.96
cykooz.resizer 0.20 39.19 76.71
cykooz.resizer - sse4_1 0.20 15.60 25.16
cykooz.resizer - avx2 0.20 11.95 18.54

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 0.28 20.73 51.07
cykooz.resizer 0.19 14.31 23.84

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.1.tar.gz (17.5 kB view details)

Uploaded Source

Built Distributions

cykooz.resizer-2.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.whl (571.9 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.5+ x86-64

cykooz.resizer-2.1-cp310-none-win_amd64.whl (522.1 kB view details)

Uploaded CPython 3.10 Windows x86-64

cykooz.resizer-2.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl (571.9 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.5+ x86-64

cykooz.resizer-2.1-cp310-cp310-macosx_10_7_x86_64.whl (559.1 kB view details)

Uploaded CPython 3.10 macOS 10.7+ x86-64

cykooz.resizer-2.1-cp39-none-win_amd64.whl (522.0 kB view details)

Uploaded CPython 3.9 Windows x86-64

cykooz.resizer-2.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (571.9 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.5+ x86-64

cykooz.resizer-2.1-cp39-cp39-macosx_10_7_x86_64.whl (559.1 kB view details)

Uploaded CPython 3.9 macOS 10.7+ x86-64

cykooz.resizer-2.1-cp38-none-win_amd64.whl (522.5 kB view details)

Uploaded CPython 3.8 Windows x86-64

cykooz.resizer-2.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (572.1 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

cykooz.resizer-2.1-cp38-cp38-macosx_10_7_x86_64.whl (559.3 kB view details)

Uploaded CPython 3.8 macOS 10.7+ x86-64

cykooz.resizer-2.1-cp37-none-win_amd64.whl (522.4 kB view details)

Uploaded CPython 3.7 Windows x86-64

cykooz.resizer-2.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (572.1 kB view details)

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

cykooz.resizer-2.1-cp37-cp37m-macosx_10_7_x86_64.whl (559.3 kB view details)

Uploaded CPython 3.7m macOS 10.7+ x86-64

File details

Details for the file cykooz.resizer-2.1.tar.gz.

File metadata

  • Download URL: cykooz.resizer-2.1.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.11

File hashes

Hashes for cykooz.resizer-2.1.tar.gz
Algorithm Hash digest
SHA256 b615ebe3ad1b50beeae147de17fac0eb8ac563e07d167611a564c60c063dd13e
MD5 09bfc5fb8f9528c76133d5dc368aff3e
BLAKE2b-256 312c54a767c8d98fb02d59bf9a49f301368cbb7242d6fce0f5226b9ae54b3105

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 58b6d897b9c61ef9a8a5d225410bebd4fc2ea04cdd3a8fcee4401fc14a7670af
MD5 4ec31501853edd384a274a2df8d6a653
BLAKE2b-256 8dc9ee2e398a576e0c26d0c1c948b76a414cebc1f9011a23090e7803d811fdab

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 de4e7c42f8ab842067dcbb265ac6ea8ca5bce5732bcd0846113a2353e62748c1
MD5 86b4e84c8e87ea04f7fcd102ca73d74f
BLAKE2b-256 033f9797a84fcff1b8b2c7db1a804d28aec6f55dc7fb9ea436548755e096581d

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ced14fa710b1b4ee558f8486407c9802be6ee1ae363a1f42b0658780d351ba76
MD5 83abe66e66f6d5d1258320307ec5dd54
BLAKE2b-256 85f5ea053297c841ebb113ecec81a88e550ac7f2450df17132d74c531f39ff72

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp310-cp310-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 7b90823bf4094eb982d142881fc9c038e0299954fa0ff8fcd8e548ecdabdfb63
MD5 103419f5dd26598bed0264c29e377e47
BLAKE2b-256 c696f940e3dce80461d856af8733885fd632217408cb13ae75541e2f674aad2e

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp39-none-win_amd64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 da90cf895dfe5395e3f4efd86c1c1af7ed463171b17a02d279367998465053dd
MD5 2f9648f0fca0e5c0bafd9cc7b7b25dae
BLAKE2b-256 d7ac6a060102626b63a533d9da6340cb8f239870b932589a89e84eed7241c9f5

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 44913e2da85cd3888e8cdc7ce532a49f98e78c15b4da80fd8c8761089277bb32
MD5 534ea1e11a1db0f4e91aac53a90ff216
BLAKE2b-256 6a98f332408f199ee1a48bb7990568642ad2869b0ada52b7c50ce733ad85dbb2

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp39-cp39-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 1549241b13dcac69e49710deaa0af141d822653cb5be707f7f2e6da3aaf46fcc
MD5 f999f0181868566c231bbd8ff713e046
BLAKE2b-256 77123084a2766575ea238906731f78d37988ed4eb38ce2861f01ee8d729a1ac5

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp38-none-win_amd64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 9ce1c020aa55008a3a1514b6a11137e7533ac433074187c29a5e064ca447e955
MD5 f12f8ce79ed02feec9aec4cc40cfd37f
BLAKE2b-256 91a54941610d85d25d3ba3a8a60b02ed5511b0c861b6a8c1e36b933f8a4afe5d

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ac5543833f9fdf09c0e1b719e05e16c645715242ef617ede8cce50265ff1497a
MD5 d2ff53b29fb7b41251db486ae0c84ec8
BLAKE2b-256 98800daabf9cedbb52a5b94bf8ad1b988a355742fa5187febadcee23365d0c68

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp38-cp38-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 6f7a33f2c5ea62332e564573ff97a8507c463de1db16a25008e4ac3612003dc3
MD5 1d3505389b815e726dd85387cfcf17d5
BLAKE2b-256 119dbbe34e59c7efc123f972953faefd5c929829abdfceb47db5137c94b28171

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp37-none-win_amd64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 67c4fd01d7487cbd9d06f690f63b73ca63b347677bd5ae07284589b21ceea27b
MD5 e509a8da15908c6b4c20a688a1f4de4d
BLAKE2b-256 f2f0cf4e8a8ddaddcd30de4f67c711376035b39927cb675b2b5caa83af4cfd6a

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 625226d1dff5d76442eee599e4f6ba1419245d9b258ee3666eaa36bbc1d1fed8
MD5 1983d86a2db012feb60e27845a983bbc
BLAKE2b-256 887b3cc250266a033267aec48128af5ac3c9ac1a2eeb3576fb067dcd778527e7

See more details on using hashes here.

File details

Details for the file cykooz.resizer-2.1-cp37-cp37m-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for cykooz.resizer-2.1-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 594b933e70968e2e0615eb3a9fb2a892c10a5f81ea028fb26ebb5cf23b98a136
MD5 28b1b0bd6bdd83ea1f083e2ddcc5c6c2
BLAKE2b-256 dea684b6fa6e6864f5540df5c8640b13f9214ba9c9626250971cfb550e2e38ce

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