{"record":{"id":"23964a65c911595a","repo":"kovidgoyal/kitty","slug":"imagemagick-failed-to-convert-correctly-it-gen","errorCode":null,"errorMessage":"ImageMagick failed to convert {} correctly, it generated {} < {} of data (w={}, h={}, bpp={})","messagePattern":"ImageMagick failed to convert (.+?) correctly, it generated (.+?) < (.+?) of data \\(w=(.+?), h=(.+?), bpp=(.+?)\\)","errorType":"exception","errorClass":"ConvertFailed","httpStatus":null,"severity":"error","filePath":"kittens/tui/images.py","lineNumber":241,"sourceCode":"            resize_cmd = ['-coalesce'] + resize_cmd + ['-deconstruct']\n        cmd += resize_cmd\n    cmd += ['-depth', '8', '-set', 'filename:f', '%w-%h-%g-%p']\n    ans = RenderedImage(m.fmt, width, height, m.mode)\n    if only_first_frame:\n        ans.frames = [Frame(m.frames[0])]\n    else:\n        ans.frames = list(map(Frame, m.frames))\n    bytes_per_pixel = 3 if m.mode == 'rgb' else 4\n\n    def check_resize(frame: Frame) -> None:\n        # ImageMagick sometimes generates RGBA images smaller than the specified\n        # size. See https://github.com/kovidgoyal/kitty/issues/276 for examples\n        sz = os.path.getsize(frame.path)\n        expected_size = bytes_per_pixel * frame.width * frame.height\n        if sz < expected_size:\n            missing = expected_size - sz\n            if missing % (bytes_per_pixel * width) != 0:\n                raise ConvertFailed(\n                    path,\n                    'ImageMagick failed to convert {} correctly, it generated {} < {} of data (w={}, h={}, bpp={})'.format(\n                        path, sz, expected_size, frame.width, frame.height, bytes_per_pixel\n                    ),\n                )\n            frame.height -= missing // (bytes_per_pixel * frame.width)\n            if frame.index == 0:\n                ans.height = frame.height\n                ans.width = frame.width\n\n    with tempfile.TemporaryDirectory(dir=os.path.dirname(output_prefix)) as tdir:\n        output_template = os.path.join(tdir, f'im-%[filename:f].{m.mode}')\n        if get_multiple_frames:\n            cmd.append('+adjoin')\n        run_imagemagick(path, cmd + [output_template])\n        unseen = {x.index for x in m}\n        for x in os.listdir(tdir):\n            try:","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/kovidgoyal/kitty/blob/6d5d0c440603ad9bdf6dcd599f73f6dde21acb44/kittens/tui/images.py#L223-L259","documentation":"Raised (as ConvertFailed) by check_resize when the output file produced by ImageMagick is smaller than width*height*bytes_per_pixel, and the missing bytes are not a whole number of rows — i.e. the file is truncated in a way that cannot be repaired by simply lowering the frame height. This works around known ImageMagick bugs that emit short files (see kitty issue #276).","triggerScenarios":"Calling render_image() on an image that ImageMagick converts incorrectly (buggy IM version, or exotic/corrupt input); check_resize detects sz < expected_size with missing % (bpp*width) != 0, so the safe height-adjustment path cannot apply.","commonSituations":"Using an old or distro-patched ImageMagick with known short-output bugs; feeding partially downloaded or truncated images; CMYK/16-bit/exotic-format inputs that IM mishandles at certain output sizes.","solutions":["Upgrade (or downgrade) ImageMagick to a version known to work, preferably IM7 'magick'","Verify/re-download the source image (file corruption) and try converting with 'magick convert' manually to reproduce","Catch ConvertFailed and skip/fallback for that image"],"exampleFix":"# before\nimg = render_image(path, width=cols)  # ConvertFailed on buggy IM\n# after\ntry:\n    img = render_image(path, width=cols)\nexcept ConvertFailed:\n    img = None  # skip preview for this file","handlingStrategy":"try-catch","validationCode":"import os\nfrom PIL import Image  # optional pre-check of source integrity\ndef source_looks_ok(path: str) -> bool:\n    try:\n        with Image.open(path) as im:\n            im.verify()\n        return True\n    except Exception:\n        return False","typeGuard":null,"tryCatchPattern":"from kittens.tui.images import ConvertFailed\ntry:\n    img = render_image(path, width=cols)\nexcept ConvertFailed as e:\n    log.warning('skipping unconvertible image %s: %s', path, e)\n    img = None","preventionTips":["Keep ImageMagick updated","Validate/re-download suspect image files","Always catch ConvertFailed in preview pipelines and skip the file"],"tags":["imagemagick","corrupt-output","image-conversion","kitty"],"backgroundTag":"external-tool-output-corrupt","analyzedSha":"6d5d0c440603ad9bdf6dcd599f73f6dde21acb44","analyzedAt":"2026-08-27T14:20:20.142Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}