From 578d537d5f15645bc49ef8b5e75e1b05b8230467 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 11 Jan 2018 09:56:40 +0530 Subject: [PATCH] kitty icat: Fix some PNG images not being displayed Work around for bug in ImageMagick where it converts images into rgba data with fewer rows than specified. Fixes #276 --- kitty/icat.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/kitty/icat.py b/kitty/icat.py index 2abecbd70..581edef7e 100755 --- a/kitty/icat.py +++ b/kitty/icat.py @@ -249,6 +249,17 @@ def convert(path, m, available_width, available_height, scale_up): cmd += ['-resize', '{}x{}'.format(width, height)] with NamedTemporaryFile(prefix='icat-', suffix='.' + m.mode, delete=False) as outfile: run_imagemagick(path, cmd + [outfile.name]) + # ImageMagick sometimes generated rgba images smaller than the specified + # size. See https://github.com/kovidgoyal/kitty/issues/276 for examples + sz = os.path.getsize(outfile.name) + bytes_per_pixel = 3 if m.mode == 'rgb' else 4 + expected_size = bytes_per_pixel * width * height + if sz < expected_size: + missing = expected_size - sz + if missing % (bytes_per_pixel * width) != 0: + raise SystemExit('ImageMagick failed to convert {} correctly, it generated {} < {} of data'.format(path, sz, expected_size)) + height -= missing // (bytes_per_pixel * width) + return outfile.name, width, height