Image Compressor for Python
Free online image compressor with Python code examples
Working with image compressor in Python? Our free online image compressor helps Python developers format, validate, and process data instantly. Below you will find Python code examples using Pillow (PIL) so you can achieve the same result programmatically in your own projects.
Try the Image Compressor Online
Use our free Image Compressor directly in your browser — no setup required.
Open Image CompressorPython Code Example
from PIL import Image
import os
def compress_image(input_path, output_path, quality=85):
img = Image.open(input_path)
# Convert RGBA to RGB for JPEG
if img.mode == 'RGBA':
img = img.convert('RGB')
# Resize if too large
max_size = (1920, 1080)
img.thumbnail(max_size, Image.LANCZOS)
# Save with compression
img.save(output_path, 'JPEG', quality=quality, optimize=True)
original = os.path.getsize(input_path)
compressed = os.path.getsize(output_path)
print(f"Compressed: {original} -> {compressed} bytes ({compressed/original*100:.1f}%)")
compress_image('input.jpg', 'output.jpg', quality=80)Quick Setup
Library: Pillow (PIL)
pip install PillowPython Tips & Best Practices
- Pillow supports JPEG, PNG, WebP, and many other formats
- Use optimize=True for additional lossless compression
- thumbnail() resizes while maintaining aspect ratio