278 | 278 |
|
279 | 279 |
try:
|
280 | 280 |
self._create_path(os.path.dirname(thumbpath))
|
281 | |
height, width = pixbuf.get_height(), pixbuf.get_width()
|
282 | 281 |
thumbnail = pixbuf
|
283 | 282 |
if isinstance(pixbuf, GdkPixbuf.PixbufAnimation):
|
284 | |
if size <= height and size <= width:
|
|
283 |
if size < pixbuf.get_width() or size < pixbuf.get_height():
|
285 | 284 |
resize_gif(mem, thumbpath, (size, size))
|
286 | 285 |
thumbnail = self._load_thumbnail(thumbpath)
|
287 | 286 |
else:
|
288 | 287 |
self._write_file(thumbpath, mem)
|
289 | 288 |
else:
|
290 | |
if size <= height and size <= width:
|
291 | |
thumbnail = pixbuf.scale_simple(
|
292 | |
size, size, GdkPixbuf.InterpType.BILINEAR)
|
|
289 |
width, height = self._get_thumbnail_size(pixbuf, size)
|
|
290 |
thumbnail = pixbuf.scale_simple(
|
|
291 |
width, height, GdkPixbuf.InterpType.BILINEAR)
|
293 | 292 |
thumbnail.savev(thumbpath, 'png', [], [])
|
294 | 293 |
except Exception as error:
|
295 | 294 |
dialogs.ErrorDialog(
|
|
301 | 300 |
log.exception(error)
|
302 | 301 |
return
|
303 | 302 |
return thumbnail
|
|
303 |
|
|
304 |
@staticmethod
|
|
305 |
def _get_thumbnail_size(pixbuf, size):
|
|
306 |
# Calculates the new thumbnail size while preserving the aspect ratio
|
|
307 |
image_width = pixbuf.get_width()
|
|
308 |
image_height = pixbuf.get_height()
|
|
309 |
|
|
310 |
if image_width > image_height:
|
|
311 |
if image_width > size:
|
|
312 |
image_height = int(size / float(image_width) * image_height)
|
|
313 |
image_width = int(size)
|
|
314 |
else:
|
|
315 |
if image_height > size:
|
|
316 |
image_width = int(size / float(image_height) * image_width)
|
|
317 |
image_height = int(size)
|
|
318 |
|
|
319 |
return image_width, image_height
|
304 | 320 |
|
305 | 321 |
@staticmethod
|
306 | 322 |
def _load_thumbnail(thumbpath):
|