Skip to main content

imgdiet

A Python package for minimizing file size of images with negligible quality loss

PNG Image WebP Image (Optimized by imgdiet)
PNG Image WebP Image
File Size: 26.9 MB File Size : 4.1 MB, Target PSNR: 40 dB
JPG Image WebP Image (Optimized by imgdiet)
PNG Image WebP Image
File Size: 1.5 MB File Size : 0.3 MB, Target PSNR: 40 dB
JPG Image WebP Image (Optimized by imgdiet)
PNG Image WebP Image
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). Use 0 for 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

  1. Lossless Compression: If the target PSNR is 0, the image is compressed in lossless WebP format.
  2. 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.
  3. 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.
  4. ICC Profile Preservation: The package retains the ICC color profile of the input image for consistent color rendering.
  5. EXIF Handling: Automatically rotates images based on their EXIF orientation metadata.
  6. 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 ImageCms module, 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:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature-name).
  3. Commit your changes (git commit -m "Add feature-name").
  4. Push to the branch (git push origin feature-name).
  5. 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.

Metadata

Release files for imgdiet 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for imgdiet 1.0.6
File Size Uploaded Provenance
imgdiet-1.0.6.tar.gz 11.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for imgdiet 1.0.6
File Interpreter ABI Platform
imgdiet-1.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 22.2 kB

Release files / imgdiet-1.0.6.tar.gz

Download URL imgdiet-1.0.6.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
645e64adbe42ef869d6bb1e4d97ecb003c212f31c596f933fa6a3c437f050994
BLAKE2b-256 checksum
How to use checksums
9514dd6a1b884b1f8fa2d0174858118c03cd795995b57cb533c6a9166b0c9c20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.11

Release files / imgdiet-1.0.6-py3-none-any.whl

Download URL imgdiet-1.0.6-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f30de3683c4f2fab48cda604fb3c9afe6e30e0153e1f642cabf11dd4862b9641
BLAKE2b-256 checksum
How to use checksums
25b45178db8995c0cf4b9c9705155b30e7e46d1a91567f74162b90198fba4306
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.11

Security for imgdiet 1.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page