crop_borders: don't crash on empty image

PIL's `Image.getbbox` documentation shows:
```
:returns: The bounding box is returned as a 4-tuple defining the
   left, upper, right, and lower pixel coordinate. See
   :ref:`coordinate-system`. If the image is completely empty, this
   method returns None.
```

when this returns `None`, komikku would error with a trace like so:
```
Traceback (most recent call last):
  File "komikku/reader/pager/image.py", line 426, in do_snapshot
    self.texture_crop = Gdk.Texture.new_for_pixbuf(crop_borders())
  File "komikku/reader/pager/image.py", line 419, in crop_borders
    if bbox[2] - bbox[0] < self.pixbuf.get_width() or bbox[3] - bbox[1] < self.pixbuf.get_height():
TypeError: 'NoneType' object is not subscriptable
```
This commit is contained in:
Colin 2023-07-12 08:31:51 +00:00
parent 7dcf2b3d0b
commit 318fc0c975
2 changed files with 2 additions and 2 deletions

View File

@ -416,7 +416,7 @@ class KImage(Gtk.Widget, Gtk.Scrollable):
# Crop is possible if computed bbox is included in pixbuf
bbox = self.crop_bbox
if bbox[2] - bbox[0] < self.pixbuf.get_width() or bbox[3] - bbox[1] < self.pixbuf.get_height():
if bbox is not None and (bbox[2] - bbox[0] < self.pixbuf.get_width() or bbox[3] - bbox[1] < self.pixbuf.get_height()):
return crop_pixbuf(self.pixbuf, bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1])
return self.pixbuf

View File

@ -409,7 +409,7 @@ class PaintablePixbuf(GObject.GObject, Gdk.Paintable):
bbox = self._compute_borders_crop_bbox()
# Crop is possible if computed bbox is included in pixbuf
if bbox[2] - bbox[0] < self.orig_width or bbox[3] - bbox[1] < self.orig_height:
if bbox is not None and (bbox[2] - bbox[0] < self.orig_width or bbox[3] - bbox[1] < self.orig_height):
return crop_pixbuf(self.pixbuf, bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1])
return self.pixbuf