A CLI tool to resize images to a target file size.
Project description
ImgByteSizer
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 FORMAT] [--min-dimension MIN_DIMENSION] [--no-exact] 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 FORMAT Output format (jpg, png, webp)
--min-dimension MIN_DIMENSION
Minimum width/height in pixels
--no-exact Do not pad file to get exact target size
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:
- First attempts to adjust quality without resizing (for formats that support quality settings)
- If quality adjustment alone isn't sufficient, performs binary search for optimal dimensions
- Fine-tunes with an additional quality optimization pass
- 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 photo.jpg
File: photo.jpg
Format: JPEG
Dimensions: 4032 ร 3024 pixels
Size: 2.5 MB
Target size: 500 KB
Trying quality adjustment without resizing...
Testing quality 48 |โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ| 100% Size: 497.3 KB
โ Found optimal quality: 48 (size: 497.3 KB)
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ Metric โ Original โ Processed โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโค
โ Dimensions โ 4032ร3024 โ 4032ร3024 โ
โ Size โ 2.5 MB โ 497.3 KB โ
โ Target Sizeโ โ 500 KB โ
โ Difference โ โ 2.7 KB (0.5%) โ
โ Reduction โ โ 80.1% smaller โ
โโโโโโโโโโโโโโดโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโ
Time taken: 0.87 seconds
Output file: photo_resized.jpg
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file imgbytesizer-0.1.0.tar.gz.
File metadata
- Download URL: imgbytesizer-0.1.0.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fae1777daaff960e93abab5232f339d53d3d8e69a197eb6a25cc523ded0f6fce
|
|
| MD5 |
1fa11a2fa13b468cd2a178dc9a76aab6
|
|
| BLAKE2b-256 |
c4a109ffb08c2090a2a736f2c3a86b0977793ae61621bbab9210a6f492189e0a
|
File details
Details for the file imgbytesizer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: imgbytesizer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17686ddc2cacaa97e542fc9710f9aab21a24b986a342ac90faca99f391f3754d
|
|
| MD5 |
9a412de6c51ef2277f8b9d33e64bcabc
|
|
| BLAKE2b-256 |
7127d21ede46fcd693efcec4e8f07cbbac5d54746eec6e1d56f324c4b75eba2f
|