跳到正文
DEV Community· Zohaib Khalid·· 24 分鐘前AI 評分55

如何用 JavaScript 在瀏覽器建立 AI 標籤移除工具,清除圖片中的 C2PA、IPTC 和 XMP

Build an AI Label Remover in the Browser: Strip C2PA, IPTC and XMP From Images With JavaScript

AI 導讀

Zohaib Khalid 示範如何用 JavaScript 在瀏覽器內清除 JPEG 和 PNG 圖片中的 C2PA、IPTC、XMP 等中繼資料,以移除依賴中繼資料的 AI 標籤。程式不會上傳圖片,並保留 ICC 色彩設定檔;由於不解碼圖像,像素不變且沒有畫質損失。這種方法只處理中繼資料,無法移除 SynthID 等像素水印,也不能改變像素式 AI 偵測結果。

正文

Instagram, Facebook and Threads add an "AI info" label. TikTok says "AI-generated", Pinterest says "AI modified", X says "Made with AI". In most cases these platforms are not analysing pixels. They read metadata inside the file.

That is why a real photo with one Generative Fill edit gets labelled, and why people search for an AI label remover or an AI image label remover. In this post we build the core of one in plain JavaScript. It runs fully in the browser, so the image is never uploaded.

What we need to remove

Signal Where it lives
C2PA Content Credentials JPEG APP11 (JUMBF boxes), PNG caBX chunk
IPTC DigitalSourceType (trainedAlgorithmicMedia) XMP packet
XMP edit history, tool names JPEG APP1 (XMP)
Exif (camera, GPS, serials, thumbnail) JPEG APP1 (Exif), PNG eXIf
Prompts (Stable Diffusion, ComfyUI) PNG tEXt / iTXt / zTXt

We keep the ICC colour profile (JPEG APP2, PNG iCCP), otherwise colours shift.

Step 1: read the file

async function readFile(file) {
  return new Uint8Array(await file.arrayBuffer());
}

Step 2: clean a JPEG

A JPEG is a sequence of segments: 0xFF, a marker byte, a 2-byte big-endian length, then the payload. Image data starts at Start of Scan (0xFFDA), and we copy everything from there unchanged.

function cleanJpeg(buf) {
  if (buf[0] !== 0xff || buf[1] !== 0xd8) throw new Error('Not a JPEG');
  const out = [buf.subarray(0, 2)];
  let i = 2;
  while (i < buf.length) {
    if (buf[i] !== 0xff) throw new Error('Bad marker');
    const marker = buf[i + 1];
    if (marker === 0xda) { out.push(buf.subarray(i)); break; } // SOS
    const len = (buf[i + 2] << 8) | buf[i + 3];
    const seg = buf.subarray(i, i + 2 + len);
    const drop =
      marker === 0xe1 ||           // APP1: Exif / XMP
      marker === 0xeb ||           // APP11: C2PA / JUMBF
      marker === 0xed ||           // APP13: Photoshop IPTC
      marker === 0xfe;             // COM: comments
    if (!drop) out.push(seg);      // keeps APP2 (ICC), DQT, SOF, DHT...
    i += 2 + len;
  }
  return concat(out);
}

Step 3: clean a PNG

After the 8-byte signature, a PNG is a list of chunks: length (4 bytes), type (4 ASCII bytes), data, CRC.

const DROP = new Set(['tEXt', 'iTXt', 'zTXt', 'eXIf', 'caBX', 'tIME']);

function cleanPng(buf) {
  const view = new DataView(buf.buffer, buf.byteOffset);
  const out = [buf.subarray(0, 8)];
  let i = 8;
  while (i < buf.length) {
    const len = view.getUint32(i);
    const type = String.fromCharCode(...buf.subarray(i + 4, i + 8));
    const end = i + 12 + len;
    if (!DROP.has(type)) out.push(buf.subarray(i, end));
    i = end;
    if (type === 'IEND') break;
  }
  return concat(out);
}

Step 4: helpers, verify and download

function concat(parts) {
  const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
  let o = 0;
  for (const p of parts) { out.set(p, o); o += p.length; }
  return out;
}

function stillHasAiTags(buf) {
  const text = new TextDecoder('latin1').decode(buf);
  return /c2pa|jumb|trainedAlgorithmicMedia|parameters|Firefly/i.test(text);
}

function download(buf, name, type) {
  const url = URL.createObjectURL(new Blob([buf], { type }));
  Object.assign(document.createElement('a'), { href: url, download: name }).click();
  URL.revokeObjectURL(url);
}

Because we never decode the image, the output has the exact same pixels and no quality loss.

Production gotchas

  • HEIC, WebP and AVIF use box or RIFF containers, so they need their own walkers.
  • Dropping all of APP1 also removes camera data. If you want to keep camera and copyright fields, parse Exif and XMP and remove only the AI-related properties.
  • Some files hold embedded thumbnails that show the unedited original. Removing Exif removes those too.

If you just want the finished tool, AI Flag Remover is a free AI label remover built this way, with HEIC/WebP/AVIF support, batch mode and a "keep camera data" option. Disclosure: I build it.

Is this an AI image detection remover?

Only for metadata-based detection. A metadata cleaner does not touch invisible pixel watermarks such as Google's SynthID, and it does not change how pixel-based AI detectors judge an image. It also cannot change a label on a post that is already live.

The honest use case is fixing false AI labels on real photos and removing private data such as GPS. If an image is AI-generated, disclose it: platforms and laws like the EU AI Act increasingly require that.

來源:DEV Community · dev.to