In real-world production scenarios, it's common to encounter situations where image files need to adhere to specific size limits when being uploaded. To address this need, our Python script offers an automated solution for efficiently reducing the size of multiple image files. This script facilitates image compression to minimize their file sizes while upholding their original quality. Images that surpass a designated size threshold are subjected to compression to ensure they fit within a specified file size limit (e.g., 1MB). Meanwhile, smaller images are left untouched and copied without undergoing any compression.
- Compress images to reduce file size while maintaining quality.
- Images exceeding a specified file size are compressed.
- Smaller images are copied without compression.
- Supports JPG, JPEG, and PNG image formats.
- Uses the Pillow library for image processing.
- Place the images you want to compress in the
inputphoto
folder. - Run the script
imagecompress.py
.
The compressed images will be saved in the output_compressed_images
folder.
- Python 3.x
- Pillow library (
pip install Pillow
)
You can configure the following parameters in the script:
max_file_size
: Maximum file size in bytes for images to be compressed.quality
: Image quality for compression (0-100).width
: New width for resized images.height
: New height for resized images.to_jpg
: Convert images to JPG format.
The script uses the Image.LANCZOS
resampling filter for resizing images, which provides high-quality results. Adjust the new_size_ratio
in the compress_img
function to achieve the desired level of compression.
Yong XIE
This project is licensed under the MIT License