Skip to main content

A CLI tool to resize images to a target file size.

Project description

ImgByteSizer

PyPI version PyPI Downloads

ImgByteSizer is a command-line tool that precisely resizes and optimizes images to match a specific file size while maintaining the best possible quality.

Features

  • ๐ŸŽฏ Resize images to an exact target file size
  • ๐Ÿ–ผ๏ธ Maintains aspect ratio during resizing
  • ๐Ÿ” Intelligent quality optimization with binary search
  • ๐Ÿ”„ Format conversion (JPEG, PNG, WebP)
  • ๐Ÿ“ Minimum dimension constraints
  • ๐ŸŽจ Beautiful terminal output with progress indicators
  • ๐Ÿ“Š Detailed comparison of original vs. processed images

Installation

pip install imgbytesizer

Usage

imgbytesizer image.jpg 500KB

Basic Examples

# Resize an image to 500KB
imgbytesizer large_photo.jpg 500KB

# Resize and convert to WebP format
imgbytesizer image.png 250KB -f webp

# Specify output file path
imgbytesizer photo.jpg 1MB -o compressed_photo.jpg

# Ensure minimum dimension is at least 400px
imgbytesizer large_image.jpg 300KB --min-dimension 400

Command-Line Options

usage: imgbytesizer [-h] [-o OUTPUT] [-f {jpg,jpeg,png,webp}] [--min-dimension MIN_DIMENSION] [--no-exact] [-v] [--debug] [-q] [image_path] [target_size]

Resize an image to match a target file size

positional arguments:
  image_path            Path to the input image
  target_size           Target file size (e.g., "1MB", "500KB")

options:
  -h, --help            show this help message and exit
  -o, --output OUTPUT   Output path (default: input_resized.ext)
  -f, --format {jpg,jpeg,png,webp}
                        Output format
  --min-dimension MIN_DIMENSION
                        Minimum width/height in pixels
  --no-exact            Do not pad file to get exact target size
  -v, --version         Show version information
  --debug               Enable debug logging
  -q, --quiet           Minimal output

How It Works

ImgByteSizer uses binary search algorithms to efficiently find the optimal combination of image dimensions and compression quality to match your target file size:

  1. First attempts to adjust quality without resizing (for formats that support quality settings)
  2. If quality adjustment alone isn't sufficient, performs binary search for optimal dimensions
  3. Fine-tunes with an additional quality optimization pass
  4. Adds minimal padding if necessary to hit the exact target size

The tool prioritizes maintaining the highest possible quality while meeting the target size constraint.

Example Output

โ— Opening image.png
  File: image.png
  Format: WEBP
  Dimensions: 1920 ร— 1080 pixels
  Size: 1.09 MB
  Target size: 250.00 KB
Trying quality adjustment without resizing...
Testing quality 95 |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘| 70% Size: 145.37 KB
โœ“ Found optimal quality: 95 (size: 145.37 KB)
Adjusting to exact target size...
Optimizing quality for exact size match...
Testing quality  96 |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 100% 

โœ“ Quality adjusted to 96: 146.07 KB
Adding 103.93 KB padding...
โœ“ File padded to exact size

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Metric      โ”‚ Original   โ”‚ Processed     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Dimensions  โ”‚ 1920ร—1080  โ”‚ 1920ร—1080     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Size        โ”‚ 1.09 MB    โ”‚ 250.00 KB     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Target Size โ”‚            โ”‚ 250.00 KB     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Difference  โ”‚            โ”‚ 0 B (0.0%)    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Reduction   โ”‚            โ”‚ 77.6% smaller โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
  Time taken: 3.77 seconds
  Output file: image_resized.webp

Real Use Cases

Web Development

# Create images that won't exceed page weight budget
imgbytesizer hero.jpg 200KB --min-dimension 1200
imgbytesizer background.png 100KB -f webp

Email Attachments

# Shrink images to fit email attachment limits
imgbytesizer family_photo.jpg 5MB

Social Media Uploads

# Optimize images for social media platforms with size limits
imgbytesizer profile_pic.jpg 400KB --min-dimension 400

Requirements

  • Python 3.9+
  • Dependencies:
    • Pillow >= 11.0.0
    • tabulate >= 0.9.0

Contributing

Contributions are welcome! Feel free to submit issues or pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

imgbytesizer-0.2.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

imgbytesizer-0.2.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: imgbytesizer-0.2.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for imgbytesizer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 42643e834cc4823d8b7746be0adbccee991182ef676301d4199ba0f8ba0f6439
MD5 927c4d07cac24d4461b37c80278064db
BLAKE2b-256 6bd757d16c87394e6ffee4ef2010f5cf3b7056083edb69e4b2cd52d5773e6076

See more details on using hashes here.

File details

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

File metadata

  • Download URL: imgbytesizer-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for imgbytesizer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 232aea3f80f6d615f72907fcd3efd4526f963d5ffe30e22bad51fa3d17a157d9
MD5 34fd1df7b2fc3dec2025713092db78cf
BLAKE2b-256 be1f88100d34722d9ed9971b82d939ba2ca3ddc349357ee9d97d56ba3d332dcd

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