Better exception for image conversion failure
This commit is contained in:
parent
5c3e4db05f
commit
2e313fbdd2
@ -18,7 +18,8 @@ from kitty.constants import appname
|
|||||||
from kitty.utils import fit_image, read_with_timeout
|
from kitty.utils import fit_image, read_with_timeout
|
||||||
|
|
||||||
from ..tui.images import (
|
from ..tui.images import (
|
||||||
NoImageMagick, OpenFailed, convert, fsenc, identify, screen_size
|
ConvertFailed, NoImageMagick, OpenFailed, convert, fsenc, identify,
|
||||||
|
screen_size
|
||||||
)
|
)
|
||||||
from ..tui.operations import clear_images_on_screen, serialize_gr_command
|
from ..tui.operations import clear_images_on_screen, serialize_gr_command
|
||||||
|
|
||||||
@ -290,6 +291,8 @@ def main(args=sys.argv):
|
|||||||
process(item, args)
|
process(item, args)
|
||||||
except NoImageMagick as e:
|
except NoImageMagick as e:
|
||||||
raise SystemExit(str(e))
|
raise SystemExit(str(e))
|
||||||
|
except ConvertFailed as e:
|
||||||
|
raise SystemExit(str(e))
|
||||||
except OpenFailed as e:
|
except OpenFailed as e:
|
||||||
errors.append(e)
|
errors.append(e)
|
||||||
if args.place:
|
if args.place:
|
||||||
|
|||||||
@ -35,6 +35,15 @@ class OpenFailed(ValueError):
|
|||||||
self.path = path
|
self.path = path
|
||||||
|
|
||||||
|
|
||||||
|
class ConvertFailed(ValueError):
|
||||||
|
|
||||||
|
def __init__(self, path, message):
|
||||||
|
ValueError.__init__(
|
||||||
|
self, 'Failed to convert image: {} with error: {}'.format(path, message)
|
||||||
|
)
|
||||||
|
self.path = path
|
||||||
|
|
||||||
|
|
||||||
class NoImageMagick(Exception):
|
class NoImageMagick(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -57,7 +66,7 @@ def identify(path):
|
|||||||
return ImageData(parts[0].lower(), int(parts[1]), int(parts[2]), mode)
|
return ImageData(parts[0].lower(), int(parts[1]), int(parts[2]), mode)
|
||||||
|
|
||||||
|
|
||||||
def convert(path, m, available_width, available_height, scale_up, tdir=None, err_class=SystemExit):
|
def convert(path, m, available_width, available_height, scale_up, tdir=None):
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
width, height = m.width, m.height
|
width, height = m.width, m.height
|
||||||
cmd = ['convert', '-background', 'none', path]
|
cmd = ['convert', '-background', 'none', path]
|
||||||
@ -78,7 +87,7 @@ def convert(path, m, available_width, available_height, scale_up, tdir=None, err
|
|||||||
if sz < expected_size:
|
if sz < expected_size:
|
||||||
missing = expected_size - sz
|
missing = expected_size - sz
|
||||||
if missing % (bytes_per_pixel * width) != 0:
|
if missing % (bytes_per_pixel * width) != 0:
|
||||||
raise err_class('ImageMagick failed to convert {} correctly, it generated {} < {} of data'.format(path, sz, expected_size))
|
raise ConvertFailed(path, 'ImageMagick failed to convert {} correctly, it generated {} < {} of data'.format(path, sz, expected_size))
|
||||||
height -= missing // (bytes_per_pixel * width)
|
height -= missing // (bytes_per_pixel * width)
|
||||||
|
|
||||||
return outfile.name, width, height
|
return outfile.name, width, height
|
||||||
@ -165,7 +174,7 @@ class ImageManager:
|
|||||||
|
|
||||||
def convert_image(self, path, available_width, available_height, image_data, scale_up=False):
|
def convert_image(self, path, available_width, available_height, image_data, scale_up=False):
|
||||||
try:
|
try:
|
||||||
rgba_path, width, height = convert(path, image_data, available_width, available_height, scale_up, tdir=self.tdir, exc_class=ValueError)
|
rgba_path, width, height = convert(path, image_data, available_width, available_height, scale_up, tdir=self.tdir)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
rgba_path = None
|
rgba_path = None
|
||||||
width = height = 0
|
width = height = 0
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user