A Python package for minimizing file size of images with minimal quality loss
Project description
imgdiet
A Python package for minimizing file size of images with negligible quality loss
| PNG Image | WebP Image (Optimized by imgdiet) |
|---|---|
| File Size: 26.9 MB | File Size : 4.1 MB, Target PSNR: 40 dB |
| JPG Image | WebP Image (Optimized by imgdiet) |
|---|---|
| File Size: 1.5 MB | File Size : 0.3 MB, Target PSNR: 40 dB |
| JPG Image | WebP Image (Optimized by imgdiet) |
|---|---|
| File Size: 2.4 MB | File Size : 1.9 MB, Target PSNR: 38 dB |
Installation
To install imgdiet, use pip:
pip install imgdiet
Alternatively, you can install the package directly from the source repository:
git clone https://github.com/developer0hye/imgdiet.git
cd imgdiet
pip install .
Features
- Compress images to the WebP format while preserving folder structure.
- Lossless and lossy compression options.
- Automatically optimizes compression level using PSNR (Peak Signal-to-Noise Ratio) targets.
- Supports multi-threaded processing for batch compression.
- Preserves ICC profiles and handles EXIF orientation.
- Supports various input formats including JPG, PNG, BMP, and TIFF.
Usage
Command-Line Interface
imgdiet provides a convenient CLI for compressing images:
imgdiet --source <input_path> --target <output_path> [--psnr <value>] [--verbose]
Arguments:
--source: Path to an image or a directory containing images.--target: Path to the output directory or a single WebP file.--psnr: Target Peak Signal-to-Noise Ratio (default: 40.0). Use0for lossless compression. Higher values mean better quality but larger file size.--verbose: Enable detailed logging for the process.
Examples:
Compress a single image:
imgdiet --source image.png --target compressed_image.webp --psnr 40.0
Compress a single image and save a compressed version in a directory:
imgdiet --source ./images/image.png --target ./compressed_images/ --psnr 40.0
Compress all images in a directory:
imgdiet --source ./images --target ./compressed_images --psnr 40.0 --verbose
Python API
You can also use imgdiet programmatically in your Python projects:
from imgdiet import save
from pathlib import Path
# Compress a single image
source_paths, target_paths = save(
source="image.png",
target="compressed_image.webp",
target_psnr=40.0,
verbose=True
)
# Returns: ([Path('image.png')], [Path('compressed_image.webp')])
# Compress a single image and save in a directory
source_paths, target_paths = save(
source="./images/image.png",
target="./compressed_images/",
target_psnr=40.0,
verbose=True
)
# Returns: ([Path('images/image.png')], [Path('compressed_images/image.webp')])
# Compress all images in a directory
source_paths, target_paths = save(
source="./images",
target="./compressed_images",
target_psnr=40.0,
verbose=False
)
# Returns: (
# [Path('images/img1.jpg'), Path('images/img2.png'), ...],
# [Path('compressed_images/img1.webp'), Path('compressed_images/img2.webp'), ...]
# )
How It Works
- Lossless Compression: If the target PSNR is
0, the image is compressed in lossless WebP format. - Lossy Compression: If the target PSNR is greater than
0, the package performs a binary search to find the optimal WebP quality that meets the PSNR target. - PSNR and Size Trade-off: Higher PSNR values result in better image quality but also larger file sizes. Conversely, lower PSNR values reduce file sizes at the cost of image quality. You can adjust the PSNR to balance quality and file size according to your needs.
- ICC Profile Preservation: The package retains the ICC color profile of the input image for consistent color rendering.
- EXIF Handling: Automatically rotates images based on their EXIF orientation metadata.
- Original Image Retention: If the compressed WebP image is larger than the original, the original image is saved instead to ensure no increase in file size.
Supported Formats
Input formats:
- JPG / JPEG
- PNG
- BMP
- TIFF / TIF
Output format:
- WebP
Requirements
- Python 3.8+
- Pillow (for image processing)
- tqdm (for progress bars in multi-threaded tasks)
Limitations
- The output format is currently limited to WebP.
- ICC profile conversion to sRGB requires the
ImageCmsmodule, which may not be available in some environments.
Future Plans
- AVIF and JPEG XL Support: We plan to add support for AVIF and JPEG XL formats in future versions. These formats offer superior compression efficiency and image quality. However, their integration requires optional dependencies since they are not natively supported by OpenCV or Pillow at this time. Once these dependencies become more accessible, they will be incorporated into
imgdiet.
Contributing
Contributions are welcome! If you'd like to contribute, please:
- Fork the repository.
- Create a feature branch (
git checkout -b feature-name). - Commit your changes (
git commit -m "Add feature-name"). - Push to the branch (
git push origin feature-name). - Open a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Enjoy compressing your images efficiently with imgdiet!
Acknowledgements
This project was developed with assistance from advanced AI tools 🤖, including ChatGPT, Claude 3.5 Sonnet, and Cursor AI, which provided guidance and feedback throughout the process.
Project details
Release history Release notifications | RSS feed
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 imgdiet-1.0.6.tar.gz.
File metadata
- Download URL: imgdiet-1.0.6.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
645e64adbe42ef869d6bb1e4d97ecb003c212f31c596f933fa6a3c437f050994
|
|
| MD5 |
54ddf30e5d4bd5638dfefe6bccf8f3fd
|
|
| BLAKE2b-256 |
9514dd6a1b884b1f8fa2d0174858118c03cd795995b57cb533c6a9166b0c9c20
|
File details
Details for the file imgdiet-1.0.6-py3-none-any.whl.
File metadata
- Download URL: imgdiet-1.0.6-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f30de3683c4f2fab48cda604fb3c9afe6e30e0153e1f642cabf11dd4862b9641
|
|
| MD5 |
e409bb11e98d537daa45dc202390d00c
|
|
| BLAKE2b-256 |
25b45178db8995c0cf4b9c9705155b30e7e46d1a91567f74162b90198fba4306
|