Skip to main content

A python package to resize images to a target file size

Project description

imagesizer

A python package to resize images to maximum file size.

Installation

imagesizer can be installed by running pip install imagesizer. It requires Python 3.9+ to run.

Usage

Import compress_to_size to your file, and use it to compress your image:

from imagesizer import compress_to_size
# Basic Example
compress_to_size("path/to/input/image.jpg", "path/to/output/image.jpg", max_size=1)

# Advanced Example
compress_to_size("path/to/input/image.jpg", "path/to/output/image.jpg", max_size=1000, unit="kb", compression_steps=1)

Parameters

  • input_image_path: Path to input image.
  • output_image_path: Path to output image.
  • max_size: Maximum size of output image (default in mb).
  • unit: Unit for max_size (default is mb). Options are 'gb', 'mb', 'kb', 'byte'.
  • compression_steps: Compression steps to take per iteration (integer, default 5). Larger steps are faster but might overcompress. Max 75 for non-PNG image, max 8 for PNG.

Limitations

  • PNG compression is limited: PNG is lossless so compression only affects how hard the algorithm works, not image quality. Large PNG files may not compress significantly below their original size.
  • Non-PNG formats use lossy compression: image quality is reduced at each compression step, so aggressive compression will visibly degrade the image.
  • compression_steps is a tradeoff: larger steps are faster but the output may end up smaller than necessary, since the algorithm cannot go back a step once it overshoots the target size.

Development

To quickly get started, run uv sync. This will automatically create a .venv directory and installs all dependencies including development dependencies. To run tests use uv run pytest tests/. For code formatting use uv run black ..

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

imagesizer-0.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

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

imagesizer-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file imagesizer-0.1.0.tar.gz.

File metadata

  • Download URL: imagesizer-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for imagesizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 99c8483195854b6685ae1b0fe35b0947b4c0b9043008e86756f4326a1bcf2b31
MD5 29395965f5516688cc44db75dd211687
BLAKE2b-256 bca3c70df1d786d84ae03411887ed6dba3316320261bdf06fc94b1ff6609eaf6

See more details on using hashes here.

File details

Details for the file imagesizer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: imagesizer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for imagesizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc6dc8495b8b50d30f09dd45f04b37362752eca5d5fff21a2bb3af4dfad0bb81
MD5 7c37a2930ca88cc7a7bfd1d04e883271
BLAKE2b-256 6298fdfd4f7a214a280a66f37b2026193225d8a7f6b9e2fe8c1dd247e35132ee

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