{"record":{"id":"12a83223f599f2b2","repo":"karatelabs/karate","slug":"latest-image-is-not-a-valid-image","errorCode":null,"errorMessage":"latest image is not a valid image","messagePattern":"latest image is not a valid image","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-image/src/main/java/io/karatelabs/ext/image/ImageComparison.java","lineNumber":111,"sourceCode":"    private final Map<String, Object> options;\n    private final Map<String, Object> result;\n\n    private ImageComparison(\n            byte[] baselineImg, byte[] latestImg, Map<String, Object> options, Map<String, Object> defaultOptions) {\n\n        this.options = options;\n        this.result = new HashMap<>();\n        this.configure(defaultOptions);\n\n        this.baselineMissing = baselineImg == null || baselineImg.length == 0;\n\n        BufferedImage baselineImage;\n        BufferedImage latestImage;\n\n        try {\n            latestImage = ImageIO.read(new ByteArrayInputStream(latestImg));\n            if (latestImage == null) {\n                throw new RuntimeException(\"latest image is not a valid image\");\n            }\n\n            baselineImage = baselineMissing ? latestImage : ImageIO.read(new ByteArrayInputStream(baselineImg));\n            if (baselineImage == null) {\n                throw new RuntimeException(\"baseline image is not a valid image\");\n            }\n        } catch (IOException e) {\n            logger.error(\"image comparison failed while reading images: {}\", e.getMessage());\n            throw new RuntimeException(e);\n        }\n\n        this.height = baselineImage.getHeight();\n        this.width = baselineImage.getWidth();\n\n        int latestHeight = latestImage.getHeight();\n        int latestWidth = latestImage.getWidth();\n\n        if (width != latestWidth || height != latestHeight) {","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-image/src/main/java/io/karatelabs/ext/image/ImageComparison.java#L93-L129","documentation":"ImageComparison's constructor decodes the latest image with ImageIO.read(); when the bytes decode to null (data is present but not a recognized image format such as PNG/JPEG/BMP/GIF), it throws this error. This is a data-content check, not a file-existence check: the bytes were found but are not decodable by the JDK's ImageIO.","triggerScenarios":"latest bytes exist but are not a valid image: empty file (0 bytes is caught as null read), corrupted/truncated download, HTML error page saved as .png, SVG (not supported by ImageIO), WebP/AVIF (unsupported by most JDKs), or a text placeholder written where a screenshot was expected.","commonSituations":"Screenshot step silently captured a blank/failed driver page; CI artifact download produced an error page; a WebP or SVG file passed where PNG was expected; image truncated by an incomplete file copy or git-lfs pointer file not materialized.","solutions":["Verify the latest file is a real PNG/JPEG (open it locally or check magic bytes with a hex dump)","Re-take the screenshot; ensure the driver/browser was ready before capture","Convert unsupported formats (WebP/SVG/AVIF) to PNG before diffing","Check for git-lfs pointer files or truncated downloads; re-download the artifact"],"exampleFix":"// before\nimage.diff({ baseline: 'base.png', latest: 'shot.webp' }) // ImageIO cannot decode WebP\n// after (convert first, e.g. via build tooling) then:\nimage.diff({ baseline: 'base.png', latest: 'shot.png' })","handlingStrategy":"validation","validationCode":"// Pre-check the latest image is a real PNG/JPEG by magic bytes\nbyte[] b = karate.readBytes('latest.png');\nboolean png = b.length > 8 && (b[0]&0xFF)==0x89 && b[1]=='P';\nif (!png) throw new RuntimeException(\"latest.png is not a valid PNG\");","typeGuard":null,"tryCatchPattern":"try {\n    image.diff({ baseline: 'base.png', latest: 'latest.png' });\n} catch (Exception e) {\n    if (String.valueOf(e).contains(\"not a valid image\")) {\n        // log bytes length / re-capture screenshot and retry once\n    }\n}","preventionTips":["Verify screenshots render locally before committing/using them","Avoid WebP/SVG/AVIF; stick to PNG for baselines and latest","Watch for git-lfs pointer files and truncated CI artifacts","Add a capture-step assertion that the screenshot file is non-empty"],"tags":["image","invalid-image","imageio"],"backgroundTag":"invalid-image-content","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}