Skip to content
Back to blog
Field notes · exif

Strip, Don't Re-encode: How to Actually Remove EXIF Without Touching a Pixel

Re-saving a photo to strip metadata re-compresses every pixel. Walking JPEG markers and PNG chunks does not. A byte-level tour of EXIF and GPS removal.

6 min readSimon Wong

Most people who care about photo metadata learn about it the same way: a screenshot of a Reddit post, a thread about how some image they uploaded gave away their home address via embedded GPS coordinates. The advice that follows is almost always the same: "open it in Photos and re-save it." That advice works - sort of - and it is the wrong tool for the job. Re-saving a JPEG runs every pixel back through a lossy DCT, re-quantises the colour planes, and adds a fresh layer of compression artefacts that are visible if you ever zoom in on a flat sky or a dark gradient. The metadata is gone; so is some of the image.

There is a much better approach, and it is the one professional tools use: walk the file's container structure and rewrite it without the metadata segments. Not a single pixel is decoded, re-quantised, or recompressed. The output is byte-identical to the input in every region that holds image data. This article is a tour of how that actually works for JPEG and PNG, what EXIF physically is inside those containers, and the one piece of metadata you almost certainly want to keep.

Two Ways to Remove Metadata, Only One Is Lossless

Every "remove EXIF" tool falls into one of two camps:

  1. Decode → re-encode. Decompress the image to a pixel buffer, encode it again with metadata flags off. This is what Image.save() in most libraries does by default, and what the macOS Photos export "without location" path does. Visually fine for one pass; visibly worse after two or three.
  2. Walk the container. Read the file as a sequence of structural blocks (markers in JPEG, chunks in PNG), copy the blocks you want byte-for-byte to a new buffer, drop the ones you don't, and write out the result. The compressed image data is never decoded.

Strategy 2 is what you want. It is also genuinely simple - the JPEG walker we ship is about 60 lines of TypeScript - because both formats are designed as flat sequences of self-describing blocks. Strategy 1 is still right when the file should also get smaller: an image compressor re-encodes on purpose, and the metadata is left behind with the old encoding.

Inside a JPEG: Markers Are Just FF Followed by a Type Byte

A JPEG file starts with FF D8 (Start Of Image) and ends with FF D9 (End Of Image). Everything in between is a sequence of markers, each beginning with 0xFF followed by a one-byte type. Most markers carry a 16-bit length field (big-endian, including the two length bytes themselves). The metadata you want to delete lives in markers FFE0-FFEF (the "APP" range) and FFFE (a free-form comment block). Image data is bracketed by FFDA (Start Of Scan).

Walking those markers takes maybe a dozen lines:

function* walkJpeg(bytes: Uint8Array) {
  if (bytes[0] !== 0xff || bytes[1] !== 0xd8) return; // not a JPEG
  let i = 2;
  while (i < bytes.length - 1) {
    if (bytes[i] !== 0xff) { i++; continue; }      // skip fill bytes
    const marker = bytes[i + 1];
    if (marker === 0xda) { yield { marker, offset: i }; return; } // SOS
    const len = (bytes[i + 2] << 8) | bytes[i + 3];
    yield { marker, offset: i, segmentLength: 2 + len };
    i += 2 + len;
  }
}

To strip metadata, copy every marker to a new buffer except the ones in 0xE0..0xEF and 0xFE, then append the original entropy-coded scan data verbatim. The output is a structurally valid JPEG that decodes to the exact same pixels - we have not touched a single byte of the DCT coefficients.

The metadata payload itself is the marker FFE1 ("APP1"). Its payload starts with the literal bytes Exif\0\0 (six bytes) and is followed by a complete TIFF file: a two-byte byte-order tag (II for little-endian, MM for big-endian), the magic 0x002A, and an offset to the first Image File Directory (IFD0). Each IFD entry is exactly 12 bytes - 2 bytes tag id, 2 bytes type, 4 bytes count, 4 bytes value-or-offset - and tags like Make (0x010F), Model (0x0110), and DateTimeOriginal (0x9003) are just numeric IDs in that table. The whole reason a photo "knows" what camera shot it is twelve bytes inside an APP1 marker.

Inside a PNG: Chunks With CRCs Already Attached

PNG is even friendlier. After an 8-byte magic header (89 50 4E 47 0D 0A 1A 0A), the file is a sequence of chunks, each one self-contained:

| 4 bytes | length of data | | 4 bytes | chunk type (ASCII, e.g. IHDR, IDAT, eXIf, tEXt) | | N bytes | data | | 4 bytes | CRC32 over (type + data) |

Image data is in IDAT. Metadata lives in eXIf (a TIFF blob, identical in structure to the JPEG APP1 payload), tEXt / iTXt / zTXt (textual key/value pairs), and tIME (a last-modified timestamp). Stripping is "copy every chunk that isn't on the metadata list, including its existing 4-byte CRC, to a new buffer." Because each chunk's CRC is stored alongside its data and you are copying both verbatim, you do not even need to recompute checksums for chunks you keep. The pixel data in IDAT is byte-identical to the input.

The fact that PNG can carry EXIF surprises a lot of people. The eXIf chunk type was only standardised in 2017, but many editors and phones now write it. If your "PNG strip" tool only deletes tEXt chunks, you may still be leaking GPS.

The GPS Trap: Three Rationals and a Direction Letter

The single most consequential tag in the whole format is GPS, and it is stored in a way that trips up most ad-hoc parsers. Latitude is not a single floating-point number. It is three RATIONAL values (degrees, minutes, seconds), each a pair of 32-bit unsigned integers (numerator / denominator), plus a one-character reference (N or S):

function gpsToDecimal(
  rationals: ReadonlyArray<[number, number]>,
  ref: "N" | "S" | "E" | "W"
): number {
  const [d, m, s] = rationals.map(([n, d]) => n / d);
  const sign = ref === "S" || ref === "W" ? -1 : 1;
  return sign * (d + m / 60 + s / 3600);
}

A naive parser that drops the reference letter will hand you a positive latitude in the southern hemisphere, mapping a Sydney photo onto a point in the South China Sea. A parser that ignores the rational denominators will think (40, 1) (44, 1) (35578, 1000) means the photo was taken at the equator. This is also why "just delete the GPS field" is often shipped as a partial fix - it leaves GPSImgDirection, GPSAltitude, GPSDateStamp, and GPSImgDirectionRef in place, any one of which still narrows your location considerably. A correct stripper drops the entire GPS IFD pointer (tag 0x8825 in IFD0), not just the latitude/longitude pair.

The One Thing You Probably Want to Keep

Almost every category of metadata can go: camera make and model, lens, exposure settings, software fingerprints, Artist, Copyright, the IPTC block, the XMP packet. There is one exception worth defending: the ICC color profile. It is technically metadata - it lives in a JPEG APP2 marker that begins with ICC_PROFILE\0, or a PNG iCCP chunk - but it tells the renderer how to interpret the colour values in the image. Strip it, and a photo encoded in Display P3 or Adobe RGB will be shown as if it were sRGB, which on a wide-gamut screen looks distinctly wrong (oversaturated reds and greens, mainly). The ICC profile reveals nothing about who took the photo or where; it is purely a colour-management instruction.

The right default is to drop everything except ICC. Our EXIF viewer & stripper does exactly that - and exposes a per-image toggle for the rare cases where you want to keep DateTimeOriginal or Orientation as well. Once your file is clean and you are about to ship it, the Base64 encoder is the natural next stop if you need to inline the image as a data URL into HTML or CSS without any extra round-trip.

Try It Yourself

Drop a photo into the EXIF viewer & stripper and watch the structure: every tag you have read about above, plus a privacy-aware GPS panel that is collapsed by default (because copy-pasting your home coordinates into a public document is the failure mode this entire problem space exists to prevent). Strip the file and re-open the result - the metadata is gone, the pixel data is untouched, and the byte difference comes entirely from the dropped markers and chunks.