// Make an enclosed pocket of background transparent, starting from a seed point inside it. // remove-background.mjs only clears background reachable from the edge (never --holes, since Mybo's belly is // nearly background-coloured), so a gap closed off by arms and a sign stays mint. Tolerance is tight on purpose. const sharp = require("module").createRequire("/home/ubuntu/kalki-cli/projects/mybazaar-web/package.json")("sharp"); const [, , file, seedArg, tolArg = "12"] = process.argv; const [sx, sy] = seedArg.split(",").map(Number), tol = +tolArg; (async () => { const { data, info } = await sharp(file).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); const W = info.width, H = info.height, s = (sy * W + sx) * 4; const ref = [data[s], data[s + 1], data[s + 2]]; const close = (p) => Math.abs(data[p * 4] - ref[0]) + Math.abs(data[p * 4 + 1] - ref[1]) + Math.abs(data[p * 4 + 2] - ref[2]) <= tol * 3; const seen = new Uint8Array(W * H), stack = [sy * W + sx]; seen[stack[0]] = 1; let cleared = 0; while (stack.length) { const p = stack.pop(); data[p * 4 + 3] = 0; cleared++; const x = p % W; for (const q of [p - W, p + W, x > 0 ? p - 1 : -1, x < W - 1 ? p + 1 : -1]) if (q >= 0 && q < W * H && !seen[q] && close(q)) { seen[q] = 1; stack.push(q); } } await sharp(data, { raw: { width: W, height: H, channels: 4 } }).png().toFile(file + ".t.png"); require("node:fs").renameSync(file + ".t.png", file); console.log(`cleared ${cleared} px of rgb(${ref}) from ${sx},${sy}`); })();