Skip to main content

Intelligent image compression library with perceptual quality optimization

Project description

Pixiq - Intelligent Image Compression

A library for intelligent image compression with perceptual quality preservation.

Key Features

  • Automatic quality selection to achieve target perceptual quality
  • Support for JPEG, WEBP, and AVIF formats
  • Image resizing capabilities
  • Resaving compressed images with new resolutions
  • Optimized hash calculation
  • Complete input parameter validation
  • Convenient methods for working with compression results

Usage Example

from PIL import Image
from pixiq import Pixiq

# Compress an image
result = Pixiq.compress(
    input=Image.open('input.jpg'),
    perceptual_quality=0.85,  # Target quality (0.0-1.0)
    max_size=2000,            # Maximum dimension size
    max_quality=95,           # Maximum compression quality
    output='output.avif'      # Output file
)

print(f'Selected quality: {result.selected_quality}')
print(f'File size: {result.file_size_kb:.1f} KB')
print(f'Dimensions: {result.dimensions}')

# Get information about the best iteration
best_iter = result.best_iteration
print(f'Best quality: {best_iter["quality"]}, error: {best_iter["error"]:.4f}')

# Resave with different resolution
result_small = result.save_thumbnail(max_size=600, output='output_600.avif')
print(f'600px file size: {result_small.file_size_kb:.1f} KB')

API Reference

Pixiq.compress()

Main method for compressing images with automatic quality selection.

Parameters:

  • input: PIL Image - input image (required)
  • perceptual_quality: float = 0.95 - target perceptual quality (0.0-1.0)
  • tolerance: float = 0.005 - quality tolerance
  • max_quality: int = None - maximum compression quality (1-100)
  • min_quality: int = None - minimum compression quality (1-100)
  • max_size: int = None - maximum image dimension
  • max_iter: int = 5 - maximum number of search iterations
  • format: str = None - output file format ('JPEG', 'WEBP', 'AVIF')
  • output: str or io.BytesIO = None - output file path or buffer

Returns: CompressionResult

Exceptions:

  • TypeError: if input parameters have incorrect types
  • ValueError: if parameter values exceed allowed ranges
  • IOError: on file saving errors

CompressionResult

Class representing the result of image compression.

Properties:

  • compressed: PIL Image - compressed image
  • iterations_count: int - number of compression iterations
  • iterations_info: list[dict] - information about each iteration
  • selected_quality: int - selected compression quality
  • hash: str - MD5 hash of compressed image
  • fmt: str - file format
  • extra_save_args: dict - additional save parameters

Computed Properties:

  • file_size_bytes: int - file size in bytes
  • file_size_kb: float - file size in kilobytes
  • dimensions: tuple[int, int] - image dimensions (width, height)
  • last_iteration: dict | None - information about the last iteration
  • best_iteration: dict | None - information about the best iteration

Methods:

  • save_thumbnail(max_size, output=None) - resaves thumbnail with new size
  • save(output) - saves image to specified output

Pixiq.save_thumbnail()

Static method for creating a thumbnail version of a compressed image.

Parameters:

  • result: CompressionResult - result of previous compression
  • max_size: int - new maximum dimension size
  • output: str or io.BytesIO = None - output file path or buffer

Returns: new CompressionResult

Supported Formats

  • JPEG: with optimization and progressive scanning
  • WEBP: with maximum compression method (method=6)
  • AVIF: with maximum speed (speed=6)

Compression Algorithm

  1. Binary search over compression quality (1-100)
  2. PSNR calculation between original and compressed images
  3. PSNR to perceptual quality conversion (empirical formula)
  4. Selection of quality with minimum error relative to target quality
  5. Optimized hash calculation from compressed buffer

Performance

  • Optimized hash calculation (without re-encoding)
  • Efficient memory usage
  • Support for large images
  • Smart file format detection

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

pixiq-0.2.0.tar.gz (93.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pixiq-0.2.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file pixiq-0.2.0.tar.gz.

File metadata

  • Download URL: pixiq-0.2.0.tar.gz
  • Upload date:
  • Size: 93.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.6

File hashes

Hashes for pixiq-0.2.0.tar.gz
Algorithm Hash digest
SHA256 957ab6ab79236c3d0742d4377997d235fce27767e496de19762c8cf0d0af9c66
MD5 2531e60a838f188cace3563b492ceb40
BLAKE2b-256 be2628a3fbae9e98dc787633ec625be668d653ebf02aa320ca160769514f1799

See more details on using hashes here.

File details

Details for the file pixiq-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pixiq-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.6

File hashes

Hashes for pixiq-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 856961847dc7021b84e563ac52e583f6020febc1247d1dc376c857e3a8d99498
MD5 95bf9a1ae30620521013a6499977427f
BLAKE2b-256 679123094e3339ed1f2de1faa3213e0079416b764200d99402ebbbb8c3511edc

See more details on using hashes here.

Supported by

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