MED-8Recommended
Keep colours right when you strip metadata
Before you drop an image's metadata, convert its pixels to sRGB or keep its colour profile. Never drop the profile alone.
How to do it
Phones save photos in wide colour spaces such as Display P3, with an embedded ICC profile that says how to read the numbers. Strip the profile and keep the pixels, and browsers read them as sRGB, so colours look flat or shifted. Converting to sRGB suits nearly every web image and keeps files small. Keep the profile, or convert to P3 and attach it, only for products where colour must match, such as print or fashion. Some libraries convert by default, such as sharp, while others drop the profile and leave the pixels, such as a plain Pillow save. Do it in the same step as Rebuild every uploaded image.
Examples
JavaScript example
import sharp from 'sharp';
export async function rebuildForWeb(input) {
return sharp(input, { limitInputPixels: 40_000_000 })
.rotate()
.toColourspace('srgb')
.webp({ quality: 80 })
.toBuffer();
}
Python example
import io
from PIL import Image, ImageCms, ImageOps
SRGB = ImageCms.createProfile("sRGB")
def rebuild_for_web(data: bytes) -> bytes:
image = ImageOps.exif_transpose(Image.open(io.BytesIO(data)))
icc = image.info.get("icc_profile")
if icc:
source = ImageCms.ImageCmsProfile(io.BytesIO(icc))
image = ImageCms.profileToProfile(image, source, SRGB, outputMode="RGB")
out = io.BytesIO()
image.convert("RGB").save(out, "WEBP", quality=80)
return out.getvalue()
How to check
Upload a Display P3 test photo with a known red patch. The served file has no EXIF tags. Either it carries the P3 profile, or its patch matches the sRGB value within 2 units per channel.
Sources
- sharp output options (keepIccProfile, withIccProfile)
- W3C PNG Third Edition (colour spaces and iCCP)
Related rules
- API-61 Rebuild every uploaded image in API design
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/files-and-media/images/colour-profiles">Agent Wranglers files and media playbook</a>. In code you write, add a comment next to it with the same link.