Sub topic 8
Common raster formats
This chapter assumes you know that a raster image is a grid of pixels, that color depth is the number of bits stored per pixel, and that compression is either lossy or lossless. It covers the four raster formats you will meet most often: JPEG, PNG, GIF and WebP.
What a format decides
A raster format is a way of writing a pixel grid to a file. Each format makes three choices:
- Compression type. Lossy formats discard some data to save space. Lossless formats keep every pixel exactly.
- Color depth. Some formats store millions of colors per pixel. Others store a palette of at most 256.
- Transparency and animation. Some formats can mark pixels as see-through or hold several frames. Others cannot.
The right format depends on what the image contains and where it will be used.
JPEG
JPEG (file extensions .jpg and .jpeg) uses lossy compression tuned for photographs. It stores 24-bit color, which is 8 bits each for red, green and blue. It divides the image into small blocks and discards fine detail that the eye notices least.
- It works well on smooth gradients and natural textures.
- It does not support transparency.
- Quality loss adds up each time you re-save the file.
- At low quality it shows blocky artifacts. Sharp text and thin lines also gather visible noise around their edges.
PNG
PNG uses lossless compression. It stores 24-bit color plus an optional alpha channel, which holds a transparency value for every pixel. It can also use a smaller 8-bit palette to cut file size.
- It keeps edges crisp, so it suits screenshots, interface graphics and images with text.
- It supports partial transparency, such as soft shadows.
- Photographs saved as PNG are often many times larger than the same photo as JPEG.
GIF
GIF is an older format limited to a palette of 256 colors per frame. It uses lossless compression, but the small palette means photos look banded and speckled. It supports animation and single-color transparency, where a pixel is either fully visible or fully hidden.
GIF is still common for short looping clips. For anything else, PNG or WebP is usually better.
WebP
WebP is a newer format that can work in either mode. It has a lossy mode, which usually gives smaller files than JPEG at similar quality. It also has a lossless mode, which usually gives smaller files than PNG. Both modes support an alpha channel, and the format supports animation.
Current browsers support it widely. Some older software, email clients and print workflows still do not, so check where the file will be opened.
Comparison
| Format | Compression | Color | Transparency | Animation | Best for |
|---|---|---|---|---|---|
| JPEG | Lossy | 24-bit | No | No | Photographs |
| PNG | Lossless | 24-bit, or 8-bit palette | Full alpha | No | Screenshots, graphics with text |
| GIF | Lossless | 256-color palette | One transparent color | Yes | Simple looping clips |
| WebP | Lossy or lossless | 24-bit | Full alpha | Yes | Web images where size matters |
Choosing a format
Common errors
- Saving a logo or screenshot as JPEG. The compression blurs edges and leaves speckles. Use PNG.
- Saving a photo as PNG. The file is far larger than it needs to be. Use JPEG or WebP.
- Editing a JPEG repeatedly. Each save loses more detail. Keep an original in a lossless format and export a JPEG only at the end.
- Using GIF for photos. The 256-color limit ruins smooth tones.
- Renaming the extension to convert. Changing photo.png to photo.jpg does not change the data. You must export or convert the file with an image tool.
Checking a file's format
The file extension can be wrong, so you can read the real format from the file's first bytes. This Python script reports it.
import sys
SIGNATURES = {
b"\xff\xd8\xff": "JPEG",
b"\x89PNG\r\n\x1a\n": "PNG",
b"GIF87a": "GIF",
b"GIF89a": "GIF",
}
def detect(path):
with open(path, "rb") as f:
head = f.read(12)
for sig, name in SIGNATURES.items():
if head.startswith(sig):
return name
if head[:4] == b"RIFF" and head[8:12] == b"WEBP":
return "WebP"
return "unknown"
if __name__ == "__main__":
print(detect(sys.argv[1]))Run it as python detect.py image.jpg. A renamed PNG will still print PNG.
Exercise
You need a product photo for a website, and a small file size matters. You also need a company logo with a soft drop shadow that sits over a colored background. Which format would you choose for each, assuming all visitors use current browsers?
Show answer
Use lossy WebP (or JPEG) for the photo, because it is a photograph and lossy compression gives small files. Use PNG or lossless WebP for the logo, because it needs full alpha transparency for the soft shadow and sharp edges. GIF cannot do the shadow because it only allows one fully transparent color.
Exercise
A colleague renames a file from banner.png to banner.jpg to make it smaller. Does this work, and how can you tell what the file really is?
Show answer
No. Renaming changes only the name, not the data, so the file is still a PNG with the same size. To check, read the first bytes: a PNG starts with the signature 89 50 4E 47. To convert it, export it as a JPEG from an image editor or conversion tool.