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 |
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 .
- 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.
imgdiet
provides a convenient CLI for compressing images:
imgdiet --source <input_path> --target <output_path> [--psnr <value>] [--verbose]
--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). Use0
for lossless compression. Higher values mean better quality but larger file size.--verbose
: Enable detailed logging for the process.
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
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'), ...]
# )
- 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.
Input formats:
- JPG / JPEG
- PNG
- BMP
- TIFF / TIF
Output format:
- WebP
- Python 3.8+
- Pillow (for image processing)
- tqdm (for progress bars in multi-threaded tasks)
- The output format is currently limited to WebP.
- ICC profile conversion to sRGB requires the
ImageCms
module, which may not be available in some environments.
- 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
.
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.
This project is licensed under the MIT License. See the LICENSE file for details.
Enjoy compressing your images efficiently with imgdiet
!
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.