A JPEG is not one compressed blob with a header bolted on. It is a sequence of labelled segments followed by the compressed image data. That layout is what makes a lossless metadata strip possible: the segments you want to delete are self-contained, and the image data does not reference them.
The distinction matters because the usual advice — open it in an editor and export again — changes the picture. It removes the metadata, and it also throws away the original compressed data.
How a JPEG is put together
The file opens with SOI, the two bytes FF D8. After that come
segments. Each one starts with FF and a marker byte; most then carry a
two-byte big-endian length that includes the two length bytes themselves. The exceptions
are standalone markers such as RST0–RST7 and EOI,
which have no length.
Application segments occupy FFE0 to FFEF and are exactly what
they sound like: space for applications to store their own data. Decoders that do not
recognise one skip it by reading its length. That is why metadata can live there without
affecting the image.
| Marker | Segment | Usually holds | Needed for pixels |
|---|---|---|---|
FFD8 | SOI | Start of image, no payload | Yes |
FFE0 | APP0 | JFIF header, density, small thumbnail | No |
FFE1 | APP1 | EXIF, including GPS and orientation | Orientation only |
FFE1 | APP1 | XMP, as RDF/XML | No |
FFE2 | APP2 | ICC colour profile, split into chunks | For colour accuracy |
FFED | APP13 | Photoshop resources, IPTC IIM records | No |
FFFE | COM | Free-text comment | No |
FFDB, FFC0, FFC4 | DQT, SOF, DHT | Quantisation tables, frame header, Huffman tables | Yes |
FFDA | SOS | Scan header, then entropy-coded data | Yes |
FFD9 | EOI | End of image | Yes |
Two APP1 segments are normal in one file: the first starts with the identifier
Exif and two null bytes, the second with the XMP namespace URI. A remover that
only looks for the first one leaves the second in place.
Why re-saving degrades the image
JPEG stores 8×8 blocks of pixels as frequency coefficients, quantised and then entropy coded. To save the file again, an editor decodes those coefficients back to samples, applies whatever change you asked for, then runs the forward transform and quantises again.
Quantisation is where the loss happens. Coefficients are divided by a table of steps and rounded to integers. Rounding is not reversible, so the second encode cannot reproduce the original values even with the same table. Save the same file ten times and the error accumulates each time — the well-known generation loss.
A "quality 100" export is still lossy. It uses a finer quantisation table, not a lossless one, and it still rounds. Editors may also resample, change chroma subsampling, convert colour spaces or drop to 8 bits per channel, any of which changes pixels beyond the compression itself.
There are only two ways to avoid it: never decode the coefficients, or transform them
losslessly. A container-level strip does the first. Tools in the jpegtran
family do the second, which is why they can rotate a JPEG without quality loss.
Stripping at the byte level
The algorithm is short. Parse markers from just after SOI. Copy every segment
you want to keep, verbatim. Skip the metadata segments. When you reach SOS,
stop parsing and copy every remaining byte through EOI unchanged.
Nothing needs recalculating. JPEG has no global offset table and no total-size field, so deleting a whole segment cannot invalidate anything after it. Each remaining segment still carries its own length. This is the opposite of a RIFF container, where the file-level size must be rewritten.
The copy-from-SOS rule is what protects the compressed data. Inside the scan, a
FF byte in the entropy stream is followed by 00 — byte stuffing —
so scanning for FFD9 to find the end of the file can match a pair inside the
image data and truncate it. Parsing by length, then copying the tail blindly, avoids that
entirely.
Progressive JPEG and other edge cases
A progressive JPEG stores the same image across several scans, each refining the last.
The frame header is SOF2 rather than SOF0, and the file contains
multiple SOS segments, each with its own header and entropy data.
This does not make stripping harder, as long as the parser stops at the first
SOS and copies the rest without interpretation. Every later scan, restart
marker and stuffed byte travels with that tail. A tool that assumes one scan and rebuilds
the file will corrupt a progressive image, which is a common reason people believe
progressive files are fragile.
Arithmetic-coded JPEG (SOF9 and SOF10) is rare but follows the
same rule. So do CMYK files with APP14 Adobe segments: keep that segment,
because it signals the colour transform, and drop only the metadata.
What must survive the strip
Keep the ICC profile in APP2 unless you have a reason not to. It is often split across several APP2 segments, each starting with the same identifier followed by a one-based sequence number and the total count. Dropping one chunk leaves an incomplete profile, which is worse than dropping all of them, because some decoders will use the fragment.
Keep orientation as well. If you delete all of APP1 without rotating the pixels, portrait
photos display sideways in every viewer that trusts the tag. The clean fix is a minimal EXIF
block holding only tag 0x0112, with no GPS table, no serial numbers and no
thumbnail.
Verifying the result
Hash the bytes from SOS to EOI before and after. If the two
hashes match, the image data was untouched, and no other check is needed to prove the strip
was lossless.
Then confirm the file shrank by exactly the sum of the removed segments. Each removed segment accounts for two marker bytes plus its declared length.
Finally, re-parse the output. Editors and "clean export" presets sometimes write a fresh
APP1 with their own Software tag, so a file can be cleaned and re-labelled in
the same save. A second pass with a tag reader is the only reliable confirmation.
If you want to compare formats, the same container-level idea applies to PNG chunks and WebP chunks, and the differences between EXIF, XMP and IPTC explain why deleting one APP1 is often not enough.
See what your photos are carrying
Drop an image in. MetaPurge lists every tag, then rewrites the file without touching a pixel.
Open the metadata removerWhere to go next
- Removing metadata from PNG — chunks, CRCs and why IDAT is never recompressed.
- Removing metadata from WebP — RIFF sizes and VP8X flags.
- GPS coordinates in photos — what the location block actually stores.