In this post, we'll explore how to reduce image size in python without losing quality using two popular libraries in the Python ecosystem (Pillow and OpenCV).
How to Reduce Image Size in Python
Getting Started
Reducing image size is a common task in web development, data processing, or machine learning workflows. Whether you're optimizing images for faster loading or preparing datasets, Python offers powerful tools to resize and compress images efficiently.
Before diving into Python code examples on how to reduce image size, let's discuss the Python libraries we will use for image size reduction.
What is Pillow?
Pillow is a popular Python Imaging Library (PIL) fork used for image processing tasks in Python. It lets you open, manipulate, and save many different image file formats (like JPEG, PNG, BMP, GIF, etc.).
Common Things You Can Do with Pillow:- Open and display images
- Resize, crop, rotate, or flip images
- Apply filters (like blur, contour, sharpen)
- Convert between image formats
- Add text or shapes to images
- Work with transparency and alpha channels
- Perform basic image enhancements (contrast, brightness, etc.)
What is OpenCV?
OpenCV (short for Open Source Computer Vision Library) is an open-source library used primarily for real-time computer vision, image processing, and machine learning. It is much more powerful and comprehensive than Pillow. It can handle:- Image Processing
- Reading/writing images
- Resizing, rotating, cropping
- Color space conversion (RGB ↔ Grayscale ↔ HSV, etc.)
- Blurring, thresholding, edge detection
- Video Processing
- Capture video from cameras or files
- Real-time video analysis
- Object tracking
- Computer Vision
- Face detection and recognition
- Object detection
- Feature matching
- Optical flow
- Contour detection
- Machine Learning
- KNN, SVM, Decision Trees
- Handwriting recognition
- Gesture recognition
Resize and Compress Images Using Pillow
Installation: pip install Pillow
Example:
from PIL import Image
# Open an image
image = Image.open('example.jpg')
# Resize the image
resized_image = image.resize((200, 200))
# Save the resized image
resized_image.save('resized_example.jpg')
Explanation:
resize(...)
: Shrinks the image dimensions.optimize=True
: Compresses the file size.quality=85
: Ranges from 1 (worst) to 95 (best). Lower means smaller size. Avoid setting quality below 70 to prevent visible artifacts.
Reduce File Size Without Changing Dimensions
If you just want to compress without resizing:
from PIL import Image
img = Image.open("example.jpg")
img.save("compressed.jpg", optimize=True, quality=70)
Resize and Compress Images Using OpenCV
Installation: pip install opencv-python
Example:
import cv2
# Load image
img = cv2.imread("example.jpg")
# Calculate new dimensions
scale_percent = 50 # percent of original size
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
# Resize
resized = cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA)
# Save with compression
cv2.imwrite("example_cv2.jpg", resized, [cv2.IMWRITE_JPEG_QUALITY, 85])
Summary
Reducing image size in Python is easy and effective using libraries like Pillow and OpenCV. These tools provide you with the control and flexibility needed for any workflow.
Thanks