跳到正文
DEV Community· adminunwatermark gemini·· 8 天前AI 評分36

AI 修補為何會破壞 Gemini 圖片角落:作者如何用 JavaScript 建立無損反向 Alpha 去水印工具

Why AI Inpainting Ruins Gemini Corners (And How I Built a Lossless Reverse-Alpha Remover in JS)

AI 導讀

作者建立 UnWatermarkGemini,並將核心引擎在 FalconX 開源,以客戶端 JavaScript 和 HTML5 Canvas API 反轉 Gemini 半透明水印的 Alpha 混合,還原原有像素。水印覆蓋範圍為 48x48 或 96x96,視乎圖片解像度;作者稱標準 2D Canvas 上的還原耗時少於 2 毫秒。

正文

Cover image for Why AI Inpainting Ruins Gemini Corners (And How I Built a Lossless Reverse-Alpha Remover in JS)

adminunwatermark gemini

If you have ever generated assets with Google Gemini (Imagen 3, Nano Banana) or Veo 3 for a UI mockup, presentation, or side project, you have probably run into the corner sparkle watermark.

Most people immediately reach for an "AI Magic Eraser" or a diffusion-based inpainting tool to clean it up. As a developer, that always bugged me. Why run a heavy, server-side neural network to hallucinate replacement pixels—usually leaving a blurry, smudged patch—when the original pixels are still mathematically sitting right there underneath the badge?

I built UnWatermarkGemini (and open-sourced the core engine under FalconX) to solve this purely with client-side JavaScript and the HTML5 Canvas API.

Here is how alpha-blended watermarks actually work under the hood, and how you can invert them with zero quality loss.


1. The Math: Inpainting vs. Reverse Alpha Blending

Unlike stock photo sites that stamp a 100% opaque logo over an image (destroying the underlying RGB values), Google Gemini applies a semi-transparent alpha-blended overlay in the bottom-right corner (48x48 or 96x96 depending on image resolution).

When any compositor renders a semi-transparent overlay onto a base image, it computes each output pixel channel ($C_{\text{out}}$) using standard linear alpha blending:

$$C_{\text{out}} = (1 - \alpha) \cdot C_{\text{orig}} + \alpha \cdot C_{\text{wm}}$$

Where:

  • $C_{\text{orig}}$ = The true, untouched pixel generated by the model
  • $C_{\text{wm}}$ = The watermark's luminance color (e.g., 255 for pure white)
  • $\alpha$ = The opacity value ($0.0 \le \alpha \le 1.0$) at that exact $(x, y)$ coordinate

Because $\alpha < 1.0$, zero background information is actually lost. Instead of guessing $C_{\text{orig}}$ with AI inpainting, we can simply solve the equation for $C_{\text{orig}}$:

$$C_{\text{orig}} = \frac{C_{\text{out}} - \alpha \cdot C_{\text{wm}}}{1 - \alpha}$$


2. Implementing the Restoration Loop in HTML5 Canvas

Once you extract the exact alpha mask (alphaMap) for the badge dimensions, restoring the corner in browser-native JavaScript takes less than 2 milliseconds on a standard 2D Canvas context:


javascript
/**
 * Restores original RGB pixel values by inverting alpha blending
 * @param {ImageData} imageData - Canvas ImageData of the corner region
 * @param {Float32Array} alphaMap - Pre-calibrated alpha values (0.0 to 1.0)
 * @param {number} wmColor - Watermark channel value (255 for white overlay)
 */
function reverseAlphaBlend(imageData, alphaMap, wmColor = 255) {
  const data = imageData.data;

  for (let i = 0, p = 0; i < data.length; i += 4, p++) {
    const alpha = alphaMap[p];

    // Skip completely transparent pixels outside the badge shape
    if (alpha <= 0.001) continue;

    // Clamp divisor to prevent division-by-zero spikes
    const invAlpha = Math.max(1 - alpha, 0.05);

    // Solve C_orig = (C_out - alpha * C_wm) / (1 - alpha) for R, G, B
    data[i]     = Math.min(255, Math.max(0, Math.round((data[i]     - alpha * wmColor) / invAlpha)));
    data[i + 1] = Math.min(255, Math.max(0, Math.round((data[i + 1] - alpha * wmColor) / invAlpha)));
    data[i + 2] = Math.min(255, Math.max(0, Math.round((data[i + 2] - alpha * wmColor) / invAlpha)));
  }

  return imageData;
}

來源:DEV Community · dev.to