Basics of image compression (differences between JPEG, PNG, and WebP and how to use them)
Image Compression Basics: Differences Between JPEG, PNG, and WebP, and When to Use Each
Introduction
When building websites or apps, have you ever wondered which image format to use?
JPEG, PNG, WebP… With so many options, it can be confusing, right? But once you understand the characteristics of each, you’ll naturally know which one to use in which situation.
In this article, we’ll explain the mechanics and proper usage of these three image formats in an easy-to-understand way.
What Is Image Compression?
Why is compression necessary?
Digital photos generate a huge amount of data when left uncompressed. If you use large images on a website…
- Page load times will slow down
- Users are more likely to leave
- Server bandwidth costs increase
That’s why we use “image compression” to reduce file sizes while preserving image quality as much as possible.
Two Types of Compression: Lossless and Lossy
Lossless compression
Compression that allows the original data to be fully restored. While image quality does not degrade, file sizes tend to be larger. It works the same way as unzipping a ZIP file.
Lossy Compression
Compression that removes some data. File sizes are significantly reduced, but the data cannot be restored to its original state. It removes parts that are difficult for the human eye to notice.
JPEG — The King of Photos
A format developed specifically for storing photos. It can significantly reduce file sizes through lossy compression. A key feature is the ability to freely adjust the compression rate using the "Quality" parameter (0–100).
- Very small file size
- Ideal for compressing photos
- Compatible with all browsers and devices
- Quality can be freely adjusted
- Cannot render transparency
- Image quality degrades with repeated saves
- Not suitable for text, lines, or illustrations
- Does not support animation
Suitable Images
PNG — The Standard for Quality and Transparency
Because it uses lossless compression, image quality does not degrade no matter how many times you save it. Another major difference from JPEG is its ability to support transparency (alpha channel). It is ideal for logos and illustrations.
- No loss of image quality
- Supports transparency
- Ideal for text, logos, and illustrations
- Quality is maintained no matter how many times it is saved
- Photos result in larger file sizes
- Does not support animations (except APNG)
- Can affect web page loading speed
Suitable images
WebP — The Optimal Solution for the Modern Web
A relatively new format developed by Google. It aims to "combine the best of JPEG and PNG," achieving image quality equal to or better than JPEG with file sizes approximately 25–35% smaller. It supports both transparency and animation.
- Files are approximately 25–35% smaller than JPEG
- Files are approximately 25% smaller than JPEG
- Supports transparency and animation
- Works with both photos and illustrations
- Compatible with all major browsers
- Not compatible with IE (older browsers)
- Difficult to edit in some design tools
- May not open in some apps, such as Windows Photo Viewer
Suitable images
Summary of the 3 formats
| Item | JPEG | PNG | WebP |
|---|---|---|---|
| Compression Method | Lossy | Lossless | Both supported |
| File size | Small | Large | Smallest |
| Image quality degradation | Yes (adjustable) | None | Virtually none |
| Transparency | Not supported | Supported | Supported |
| Animation | Not supported | Not supported | Supported |
| Portrait orientation | ◎ Best | △ Not suitable | ◎ Best |
| Suitable for illustrations and logos | △ Not suitable | ◎ Ideal | ○ Good |
| Browser Compatibility | ◎ Fully supported | ◎ Fully supported | ○ Supports major browsers |
Practical Guide: How to Choose
Recommendations by Scenario
| Scenario | Recommendations | Reason |
|---|---|---|
| Website Photos | WebP (Runner-up: JPEG) | Small file size and fast loading |
| Company Logo / Site Logo | PNG / SVG | Supports transparency and no loss of quality |
| Screenshots | PNG | Text appears crisp |
| Product Images (E-commerce) | WebP (Runner-up: JPEG) | Good balance between image quality and loading speed |
| Banners (with text) | PNG / WebP | Text does not blur |
| Email attachments | JPEG | High compatibility |
| App icons | PNG / WebP | Supports transparency and small file sizes |
| Alternative to animated GIFs | WebP | Significantly smaller file size |
Decision Flowchart
How to handle it in code
Embedding images in HTML
To prioritize WebP while providing JPEG to unsupported browsers, <picture> Use the tag.
<!-- pictureタグを使ったフォールバック --> <picture> <!-- WebP対応ブラウザはこちらを使う --> <source srcset="image.webp" type="image/webp"> <!-- 非対応ブラウザはJPEGに切り替わる --> <source srcset="image.jpg" type="image/jpeg"> <!-- 最後はimg要素(必須) --> <img src="image.jpg" alt="説明文"> </picture>
Writing it this way ensures that WebP-compatible browsers display the fast WebP version, while non-compatible browsers automatically display the highly compatible JPEG version.
Converting to WebP in Python
You can convert images in just a few lines using Python's Pillow (PIL) library. To install it, pip install Pillow here.
from PIL import Image # JPEG を WebP に変換(非可逆、quality=85 が一般的) img = Image.open("photo.jpg") img.save("photo.webp", "WEBP", quality=85) # PNG を WebP(透過あり)に変換(可逆) img = Image.open("logo.png") img.save("logo.webp", "WEBP", lossless=True) # フォルダ内の全JPEGをまとめて変換 import os for filename in os.listdir("./images"): if filename.endswith(".jpg"): img = Image.open(f"./images/{filename}") name = filename.replace(".jpg", ".webp") img.save(f"./output/{name}", "WEBP", quality=85) print(f"変換完了: {name}")
Summary
A versatile format with small file sizes that works worldwide. However, it does not support transparency, and image quality degrades with each save.
Supports transparency and lossless saving. Ideal for logos, illustrations, and screenshots. Photo files tend to be larger.
Combines the best of both JPEG and PNG while minimizing file size. Make this your first choice for new projects.
I hope this article helps you understand image formats.