Most "remove metadata" snippets you find draw the photo onto a <canvas> and export it again. That works, but it re-compresses the image: you lose a bit of quality, the file size changes, and you get whatever JPEG encoder the browser ships.
If all you want is to drop the camera model, timestamps and GPS coordinates, you don't have to touch the pixels at all. A JPEG is a sequence of tagged segments, and the metadata lives in its own segments. Skip those, copy everything else byte-for-byte, done.
How a JPEG is laid out
- It starts with
FF D8(SOI, start of image). - Then come segments:
FF, a marker byte, and a 2-byte big-endian length (the length counts itself but not theFF xxmarker). -
FF DA(SOS, start of scan) begins the compressed image data. From there to the end, we copy everything as-is.
The segments we care about before SOS:
| Marker | Name | What's inside | Keep? |
|---|---|---|---|
FF E0 | APP0 | JFIF header | keep |
FF E1 | APP1 | Exif (camera, dates, GPS, orientation) or XMP | drop |
FF E2 | APP2 | usually the ICC color profile | keep (colors shift without it) |
FF ED | APP13 | Photoshop / IPTC (captions, author, location text) | drop |
FF FE | COM | free-text comment | drop |
FF DB, FF C0, FF C4... | tables / frame header | needed to decode | keep |
The code
// Returns an array of Uint8Array slices you can pass straight to new Blob() function stripJpegMetadata(bytes) { const src = new Uint8Array(bytes); if (src[0] !== 0xFF || src[1] !== 0xD8) throw new Error('Not a JPEG'); const DROP = new Set([0xE1, 0xED, 0xFE]); // APP1 (Exif/XMP), APP13 (IPTC), COM const parts = [src.subarray(0, 2)]; // keep SOI let i = 2; while (i < src.length) { if (src[i] !== 0xFF) throw new Error('Unexpected byte at offset ' + i); while (src[i + 1] === 0xFF) i++; // skip optional fill bytes const marker = src[i + 1]; if (marker === 0xDA) { // SOS: rest is image data parts.push(src.subarray(i)); break; } const length = (src[i + 2] << 8) | src[i + 3]; const end = i + 2 + length; if (!DROP.has(marker)) parts.push(src.subarray(i, end)); i = end; } return parts; } subarray doesn't copy, so this is cheap even for 10 MB phone photos.
Wiring it to a file input
<input type="file" id="pick" accept="image/jpeg"> <script type="module"> document.getElementById('pick').addEventListener('change', async (e) => { const file = e.target.files[0]; if (!file) return; const parts = stripJpegMetadata(await file.arrayBuffer()); const clean = new Blob(parts, { type: 'image/jpeg' }); const a = document.createElement('a'); a.href = URL.createObjectURL(clean); a.download = file.name.replace(/\.jpe?g$/i, '') + '-clean.jpg'; a.click(); URL.revokeObjectURL(a.href); }); </script> Nothing leaves the machine: the file is read, sliced and saved locally.
Checking that it worked
I tested it with a small JPEG that had an orientation tag, a camera make, a GPS block and a comment. Afterwards Pillow reported an empty Exif dict and no comment, and the decoded pixels were identical to the original (no re-encode). You can check your own files with:
exiftool photo-clean.jpg # GPS*, Make, Model, DateTimeOriginal should be gone Gotchas
1. Orientation disappears too. Phones often store the sensor image sideways and set the Exif Orientation tag (e.g. 6 = rotate 90° CW). Browsers honor that tag by default, so after stripping, a portrait photo can suddenly show up lying on its side. Options:
- rotate the pixels first (which means re-encoding), or
- write back a tiny APP1 that contains only the Orientation tag, or
- accept it for images you know are already upright (screenshots, most edited exports).
2. JPEG only. PNG keeps metadata in tEXt/iTXt/eXIf chunks, WebP in EXIF/XMP chunks inside RIFF, HEIC in a completely different box structure. Same idea (walk the container, skip the metadata blocks), different parser.
3. Malformed files. Real-world JPEGs are sometimes truncated or have junk between segments. The loop above throws on anything unexpected; in production you'd want a fallback (e.g. the canvas re-encode) rather than failing.
4. Metadata isn't only in the file. Stripping Exif doesn't touch what's in the picture itself (street signs, screens, reflections), and it doesn't change the file name.
When the canvas approach is still fine
If you're resizing or compressing anyway, you're re-encoding regardless, and canvas.toBlob() drops metadata as a side effect. The byte-level approach is for when you want the exact same image, just without the extra data.
Disclosure: I wrote this with help from an AI assistant. I ran the code and checked the results myself before publishing.