gchq/CyberChef · error · OperationError

Error inverting image. (${err})

Error message

Error inverting image. (${err})

What it means

Catches any exception during image.invert() or getBuffer and re-wraps it. invert() is robust for most images, so this usually means a Jimp internal failure, a corrupted pixel buffer, or a getBuffer encode error (GIF outputs are re-encoded as PNG). The interpolated err carries the Jimp cause.

Source

Thrown at src/core/operations/InvertImage.mjs:63

        try {
            image = await Jimp.read(input);
        } catch (err) {
            throw new OperationError(`Error loading image. (${err})`);
        }
        try {
            if (isWorkerEnvironment())
                self.sendStatusMessage("Inverting image...");
            image.invert();

            let imageBuffer;
            if (image.mime === "image/gif") {
                imageBuffer = await image.getBuffer(JimpMime.png);
            } else {
                imageBuffer = await image.getBuffer(image.mime);
            }
            return imageBuffer.buffer;
        } catch (err) {
            throw new OperationError(`Error inverting image. (${err})`);
        }
    }

    /**
     * Displays the inverted image using HTML for web apps
     * @param {ArrayBuffer} data
     * @returns {html}
     */
    present(data) {
        if (!data.byteLength) return "";
        const dataArray = new Uint8Array(data);

        const type = isImage(dataArray);
        if (!type) {
            throw new OperationError("Invalid file type.");
        }

        return `<img src="data:${type};base64,${toBase64(dataArray)}">`;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Downscale the image and retry to rule out memory/timeout.
  2. Re-encode the source to PNG so the encode path is clean.
  3. If err names a Jimp method, pin/upgrade jimp.
  4. Validate the image opens in an image editor before processing.

Example fix

// before
invertOp.run(hugeCorruptPng, []); // -> Error inverting image.
// after
const safe = await reencodeToPng(hugeCorruptPng, { maxWidth: 4000 });
invertOp.run(safe, []);
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await invertOp.run(buf, []);
} catch (e) {
  if (e instanceof OperationError && /Error inverting image/.test(e.message)) {
    // Likely oversized/corrupt pixels: downscale or re-encode, then retry.
    await retryWithReencodedPng(buf);
  } else throw e;
}

Prevention

When it happens

Trigger: Jimp internal failure on an unusual pixel layout, getBuffer failing during PNG re-encode, or memory exhaustion on a very large image during the invert pass.

Common situations: Oversized image, corrupted pixels surviving read, or a jimp version bug on a specific codec.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/cf20ec04b1ea2edf. Report an issue: GitHub.