Python Image Compression: Choosing a Format for Property Photos
In this article
- Why Format Matters for Property Photos
- Common Image Formats Explained
- How Compression Works in Python
- How to Choose the Right Format
- Common Problems and Solutions
- Comparing File Sizes and Quality
- Practical Python Script for Batch Compression
- Costs and Performance Considerations
- Alternatives to Pillow
- Frequently Asked Questions
For property photos, the best Python image compression format is JPEG for standard listings and WebP for web use when you need smaller file sizes without visible quality loss. This article explains how to choose between JPEG, PNG, WebP, and AVIF, and provides practical Python code examples using Pillow and other libraries.
Why Format Matters for Property Photos
Property photos are the first thing buyers see. A blurry or slow-loading image can deter potential buyers. The format you choose affects file size, loading speed, and image quality. Real estate portals often have file size limits, and faster loading pages rank better in search results. The right format balances quality and size, ensuring your photos look sharp while uploading quickly.
Common Image Formats Explained
- JPEG – The standard for photographs. Uses lossy compression, great for complex scenes like interiors. File sizes are small, but quality degrades with repeated saving.
- PNG – Lossless compression, best for graphics with text or sharp edges. For property photos, PNG files are large and rarely worth it unless you need transparency.
- WebP – Modern format by Google. Supports both lossy and lossless. Lossy WebP is 25–34% smaller than JPEG at similar quality, making it ideal for web listings.
- AVIF – Newer format based on AV1 video codec. Even better compression than WebP, but browser support is still limited, and encoding is slower.
How Compression Works in Python
Python's Pillow library is the most common tool for image compression. It wraps the libjpeg and libwebp libraries. When you save a JPEG, you specify a quality parameter (0-95). A higher quality means less compression and a larger file. For WebP, the quality parameter works similarly, but the compression algorithm is more efficient, so you can use a lower quality value while keeping visual fidelity.
Here's a simple example using Pillow:
from PIL import Image
img = Image.open('photo.jpg')
img.save('photo_compressed.jpg', quality=85, optimize=True)
img.save('photo_compressed.webp', quality=80, method=6)
The optimize=True flag for JPEG uses a more efficient encoding, and method=6 for WebP gives the best compression (slower).
How to Choose the Right Format
Follow these steps for property photos:
- Determine the use case. If the photo goes on a listing site that accepts JPEG only, use JPEG. If you control the website, use WebP.
- Check file size limits. Most portals allow 10-20 MB per photo, but smaller is better for speed. Aim for under 500 KB per image for web.
- Test quality. Use a quality of 80-85 for JPEG, and 75-80 for WebP. Compare visually to ensure no artifacts.
- Consider transparency. If you need a transparent background (rare for property photos), use PNG or WebP with alpha.
- Automate with a script. Use Python to batch compress all photos in a folder.
Common Problems and Solutions
Problem: JPEG artifacts in skies or smooth walls. Solution: Use a higher quality (90+) or switch to WebP which handles gradients better.
Problem: Large PNG files. Solution: Convert to JPEG or WebP unless transparency is required.
Problem: WebP not supported by some older systems. Solution: Provide fallback JPEG using the <picture> element in HTML.
Problem: Slow encoding for AVIF. Solution: Use WebP for speed, or use the pillow-avif-plugin if AVIF is mandatory.
Comparing File Sizes and Quality
For a typical 4000x3000 pixel interior photo (about 5 MB raw), the compressed sizes are roughly:
- JPEG quality 85: ~800 KB
- WebP quality 80: ~550 KB
- AVIF quality 40: ~400 KB
- PNG: ~10 MB (not recommended)
These are estimates; actual sizes vary with image complexity. The human eye often cannot tell the difference between JPEG 85 and WebP 80, but the file size difference is significant, especially when a listing has 20 photos.
Practical Python Script for Batch Compression
Here's a complete script that compresses all images in a folder to WebP and JPEG, keeping the original:
import os
from PIL import Image
input_folder = 'original'
output_folder = 'compressed'
os.makedirs(output_folder, exist_ok=True)
for filename in os.listdir(input_folder):
if filename.lower().endswith(('.jpg', '.jpeg', '.png')):
img = Image.open(os.path.join(input_folder, filename))
base = os.path.splitext(filename)[0]
# Save WebP
img.save(os.path.join(output_folder, base + '.webp'), 'WEBP', quality=80, method=6)
# Save JPEG
img.save(os.path.join(output_folder, base + '.jpg'), 'JPEG', quality=85, optimize=True)
print('Done')
This script preserves the original files and creates compressed versions. You can adjust the quality values based on your needs.
Costs and Performance Considerations
Compressing images in Python is free and uses local CPU. For large batches, the process may take time. WebP encoding with method=6 is slower than JPEG, but for a typical batch of 100 photos, it takes only a few minutes. If you need faster processing, reduce the method to 4 or use a lower quality. There are no licensing fees for using these formats.
Alternatives to Pillow
Besides Pillow, you can use:
- OpenCV – Fast, but mainly for computer vision, not ideal for saving high-quality images.
- imageio – Supports many formats, but less control over compression parameters.
- sharp (via pybind) – Uses libvips, very fast and memory-efficient for large images.
- Cloud services – Like AWS Rekognition or Cloudinary, but they cost money and require uploading images.
For most property photo workflows, Pillow is sufficient and the easiest to use.
Frequently Asked Questions
Q: Can I use PNG for property photos? A: Yes, but only if you need transparency or text overlays. PNG files are large and slow to load, which hurts user experience.
Q: What is the best quality setting for JPEG? A: Use 85 for web. Going above 90 rarely improves visible quality but increases file size significantly.
Q: How do I reduce file size without losing quality? A: Use WebP with quality 80. It gives the same visual quality as JPEG 85 but with a smaller file size.
Q: Is AVIF worth using? A: Only if your target browsers support it (Chrome, Firefox, Safari 16+). Otherwise, stick with WebP.
Q: Can I compress images in Python without losing quality? A: Lossless compression is possible with PNG or WebP lossless, but file sizes remain large. For property photos, lossy compression with quality 80-85 is visually lossless.
Q: How do I check the file size after compression? A: Use os.path.getsize('file.jpg') in Python to see the size in bytes.