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.1.1.tar.gz (91.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.1.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pixiq-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7d8265b18ca36ff0b9c2deb1f58e4f4845aab7b5fd9dbcb607bf15db1f5e52da
MD5 1fece70015f45dba79420465e8960a6c
BLAKE2b-256 fe2bef096a574b826fc11963323218683299a143786c6834b961dc43f7987c49

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pixiq-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aa5aaa2816158217fc444118884a29a1a55a10dc04387b72d79853bf8e3d64c0
MD5 9d43d53cb6a5c0e355a71d2c4b3e89d5
BLAKE2b-256 1a579fca53853cf46c65c6d2ada41f474eeb70e783556d9a495d8fd1eeda7afa

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