karatelabs/karate · error · RuntimeException

baseline image is not a valid image

Error message

baseline image is not a valid image

What it means

Same decode check as the latest image but applied to the baseline: ImageIO.read() returned null for the baseline bytes, meaning the baseline exists but is not a decodable image. Note: when the baseline is missing entirely the code reuses the latest image instead, so this error only fires when a baseline was provided yet is unreadable as an image.

Solutions

  1. Open the baseline file locally to confirm it renders; replace it if corrupt
  2. Re-capture the baseline in a controlled environment and commit the valid PNG
  3. Verify git-lfs files materialized (git lfs pull) rather than pointer text
  4. Convert the baseline to a JDK-supported format (PNG/JPEG/BMP/GIF)

Example fix

// before
image.diff({ baseline: 'base.svg', latest: 'shot.png' }) // SVG not decodable
// after
image.diff({ baseline: 'base.png', latest: 'shot.png' })
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check baseline decodability before diffing
byte[] b = karate.readBytes('base.png');
boolean png = b.length > 8 && (b[0]&0xFF)==0x89 && b[1]=='P';
if (!png) throw new RuntimeException("baseline base.png is not a valid PNG");

Try / catch

try {
    image.diff({ baseline: 'base.png', latest: 'latest.png' });
} catch (Exception e) {
    if (String.valueOf(e).contains("baseline image is not a valid image")) {
        // regenerate the baseline in a controlled environment
    }
}

Prevention

When it happens

Trigger: Providing a baseline whose bytes are corrupt, empty, an HTML error page, SVG, WebP, or otherwise not decodable by ImageIO; a checked-in baseline that was overwritten or truncated; git-lfs not materializing the file so the pointer text is read.

Common situations: Team checked in a corrupted baseline or a placeholder; CI checkout skipped LFS smudge; someone committed a screenshot with a wrong extension; baseline regenerated by a failed tooling run.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/3db6b0e9921a21cf. Report an issue: GitHub.

Appendix: source

Thrown at karate-image/src/main/java/io/karatelabs/ext/image/ImageComparison.java:116

        this.options = options;
        this.result = new HashMap<>();
        this.configure(defaultOptions);

        this.baselineMissing = baselineImg == null || baselineImg.length == 0;

        BufferedImage baselineImage;
        BufferedImage latestImage;

        try {
            latestImage = ImageIO.read(new ByteArrayInputStream(latestImg));
            if (latestImage == null) {
                throw new RuntimeException("latest image is not a valid image");
            }

            baselineImage = baselineMissing ? latestImage : ImageIO.read(new ByteArrayInputStream(baselineImg));
            if (baselineImage == null) {
                throw new RuntimeException("baseline image is not a valid image");
            }
        } catch (IOException e) {
            logger.error("image comparison failed while reading images: {}", e.getMessage());
            throw new RuntimeException(e);
        }

        this.height = baselineImage.getHeight();
        this.width = baselineImage.getWidth();

        int latestHeight = latestImage.getHeight();
        int latestWidth = latestImage.getWidth();

        if (width != latestWidth || height != latestHeight) {
            if (toBool(defaultOptions.get("allowScaling"))) {
                Image scaledImage = latestImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
                latestImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                latestImage.getGraphics().drawImage(scaledImage, 0, 0, null);
                latestHeight = height;

View on GitHub (pinned to a22eb90246)