GrepBoard/Manuals
605 manuals ·

Shrink an AI-generated PNG to WebP and check its metadata

·Tested on Ubuntu 24.04.5 · cwebp 1.3.2 · Pillow 13.0.0 · ExifTool 12.76 · C2PA spec 2.2·Test scope ↓·6 steps

Convert a 1024×1024 PNG with cwebp at several quality values, measure the sizes, then see whether the prompt, EXIF, XMP and C2PA data survive.

PNGs from image generators often run past 1 MB each. The numbers below come from a synthetic image, not real AI output: a 1024×1024 picture drawn with Pillow (gradient, shapes, light noise) with a fake generation record (parameters) written into a PNG text chunk. Real generated images carry more detail and will shrink less than the table shows.

[1] Check the size and metadata before converting. exiftool -a -G1 -s lists duplicate tags too, with their group names.

console
$ ls -l sample.png | awk '{print $5, $9}'1184103 sample.png$ exiftool -a -G1 -s sample.png | grep -E 'Parameters|CreatorTool|ImageDescription|Software'[PNG]           Parameters                      : prompt: a yellow sun over blue hills, flat illustration.Negative prompt: text, watermark.Steps: 30, Sampler: Euler a, CFG scale: 7, Seed: 12345, Size: 1024x1024, Model: example-model[XMP-xmp]       CreatorTool                     : example-generator 1.0[IFD0]          ImageDescription                : synthetic test image[IFD0]          Software                        : example-generator 1.0

[2] Convert with cwebp. -q takes 0–100 and defaults to 75. The sizes below come from converting the same PNG with only -q changed; PSNR is the overall value cwebp prints against the source.

console
$ cwebp -quiet -q 80 sample.png -o q80.webp
OptionBytesvs. PNGPSNR (dB)
(source PNG)1,184,103100%—
-lossless756,49863.9%—
-q 9081,4286.9%45.74
-q 8017,6501.5%43.89
-q 7513,7461.2%43.34
-q 6011,9161.0%42.95
-q 409,1700.8%42.11

A drawing with large flat areas compresses unusually well, so treat the shape, not the values: size drops more than fourfold from q 90 to q 80, then flattens out. To go smaller still, add -resize 512 512; the -q 80 file drops from 17,650 to 6,270 bytes.

[3] Check what the converted WebP kept. With default options none of the four lines above are present.

console
$ exiftool -s -G1 -XMP:all -IFD0:all q80.webp

The output is empty. The -metadata default is none, so the prompt, EXIF and XMP are all dropped.

[4] To keep metadata, name the kinds in -metadata. Valid values: all, none, exif, icc, xmp.

console
$ cwebp -quiet -q 80 -metadata all sample.png -o m_all.webp$ exiftool -a -G1 -s m_all.webp | grep -E 'WebP_Flags|XMP'[RIFF]          WebP_Flags                      : XMP[XMP-xmp]       CreatorTool                     : example-generator 1.0

Only XMP survived in this test. The EXIF (ImageDescription, Software) and the Parameters text chunk did not come along. The cause was not checked. To keep the prompt from a text chunk, read the value and write it back as XMP or EXIF.

[5] To keep EXIF and XMP reliably, convert with Pillow. Pass exif and xmp to save().

console
$ python3 - <<'PY'from PIL import Imageim = Image.open("sample.png")im.save("pil_q80_exif.webp", quality=80,        exif=im.getexif().tobytes(),        xmp=im.info["XML:com.adobe.xmp"].encode())PY$ exiftool -a -G1 -s pil_q80_exif.webp | grep -E 'WebP_Flags|XMP|IFD0'[RIFF]          WebP_Flags                      : XMP, EXIF[IFD0]          ImageDescription                : synthetic test image[IFD0]          Software                        : example-generator 1.0[XMP-xmp]       CreatorTool                     : example-generator 1.0

The file with metadata is 17,988 bytes, 338 more than the one without (17,650). To strip it again, run exiftool -all= -o newfile source.

[6] Decide about C2PA provenance data. If the generator embedded a C2PA manifest (Content Credentials), the picture changes. C2PA spec 2.2 stores it in PNG as a caBX chunk, defined as an "ancillary, private, not safe to copy" chunk, and in WebP as a RIFF chunk with the identifier C2PA. "Not safe to copy" is PNG's marker that a tool which modifies the image need not copy the chunk.

For the test, a fake caBX chunk (content PLACEHOLDER-NOT-A-REAL-C2PA-MANIFEST) was inserted into the PNG and converted with cwebp -metadata all. The resulting WebP does not contain it. It is not a real manifest, so no signature validation was done; all that was confirmed is that cwebp does not carry this chunk over.

One line to decide by: drop the prompt and seed unless you have a reason to publish them; drop C2PA only knowing that it is the "this was made by AI" marker, and it goes with it.

#webp#image#metadata