From 9d1d99c5c648c914cb1cfc21586a7f10fbcd597f Mon Sep 17 00:00:00 2001 From: Paul Friederichsen Date: Sat, 16 Mar 2024 14:38:22 -0500 Subject: [PATCH] Center default thumbnails inside target resolution --- hydrus/core/files/HydrusFileHandling.py | 57 +++++++------------ .../core/files/images/HydrusImageHandling.py | 10 ++++ 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/hydrus/core/files/HydrusFileHandling.py b/hydrus/core/files/HydrusFileHandling.py index 216f25a5..8a632638 100644 --- a/hydrus/core/files/HydrusFileHandling.py +++ b/hydrus/core/files/HydrusFileHandling.py @@ -90,6 +90,13 @@ mimes_to_default_thumbnail_paths[ HC.APPLICATION_PROCREATE ] = os.path.join( HC. mimes_to_default_thumbnail_paths[ HC.APPLICATION_RTF ] = os.path.join( HC.STATIC_DIR, 'rtf.png' ) mimes_to_default_thumbnail_paths[ HC.IMAGE_SVG ] = os.path.join( HC.STATIC_DIR, 'svg.png' ) +def GenerateDefaultThumbnail( mime: int, target_resolution: typing.Tuple[ int, int ] ): + + thumb_path = mimes_to_default_thumbnail_paths[mime] + + return HydrusImageHandling.GenerateDefaultThumbnailNumPyFromPath( thumb_path, target_resolution ) + + def GenerateThumbnailBytes( path, target_resolution, mime, duration, num_frames, percentage_in = 35 ): thumbnail_numpy = GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, percentage_in = percentage_in ) @@ -130,9 +137,7 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, PrintMoreThumbErrorInfo( e, f'Problem generating thumbnail for "{path}".', extra_description = extra_description ) - thumb_path = os.path.join( HC.STATIC_DIR, 'zip.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) finally: @@ -153,9 +158,7 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, PrintMoreThumbErrorInfo( e, f'Problem generating thumbnail for "{path}".', extra_description = extra_description ) - thumb_path = os.path.join( HC.STATIC_DIR, 'clip.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) finally: @@ -171,10 +174,8 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, except Exception as e: PrintMoreThumbErrorInfo( e, f'Problem generating thumbnail for "{path}".', extra_description = extra_description ) - - thumb_path = os.path.join( HC.STATIC_DIR, 'krita.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) elif mime == HC.APPLICATION_PROCREATE: @@ -191,9 +192,7 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, PrintMoreThumbErrorInfo( e, f'Problem generating thumbnail for "{path}".', extra_description = extra_description ) - thumb_path = os.path.join( HC.STATIC_DIR, 'procreate.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) finally: @@ -224,9 +223,7 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, PrintMoreThumbErrorInfo( e, f'Secondary problem generating thumbnail for "{path}".', extra_description = extra_description ) - thumb_path = os.path.join( HC.STATIC_DIR, 'psd.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) finally: @@ -244,9 +241,7 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, PrintMoreThumbErrorInfo( e, f'Problem generating thumbnail for "{path}".', extra_description = extra_description ) - thumb_path = os.path.join( HC.STATIC_DIR, 'svg.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) elif mime == HC.APPLICATION_PDF: @@ -259,9 +254,7 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, PrintMoreThumbErrorInfo( e, f'Problem generating thumbnail for "{path}".', extra_description = extra_description ) - thumb_path = os.path.join( HC.STATIC_DIR, 'pdf.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) elif mime == HC.APPLICATION_PPTX: @@ -274,9 +267,7 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, PrintMoreThumbErrorInfo( e, f'Problem generating thumbnail for "{path}".', extra_description = extra_description ) - thumb_path = os.path.join( HC.STATIC_DIR, 'pptx.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) elif mime == HC.APPLICATION_FLASH: @@ -292,9 +283,7 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, PrintMoreThumbErrorInfo( e, f'Problem generating thumbnail for "{path}".', extra_description = extra_description ) - thumb_path = os.path.join( HC.STATIC_DIR, 'flash.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) finally: @@ -313,9 +302,7 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, PrintMoreThumbErrorInfo( e, f'Problem generating thumbnail for "{path}".', extra_description = extra_description ) - thumb_path = os.path.join( HC.STATIC_DIR, 'hydrus.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) elif mime == HC.ANIMATION_UGOIRA: @@ -336,9 +323,7 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, PrintMoreThumbErrorInfo( e, f'Problem generating thumbnail for "{path}".', extra_description = extra_description ) - thumb_path = os.path.join( HC.STATIC_DIR, 'zip.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) finally: @@ -393,9 +378,7 @@ def GenerateThumbnailNumPy( path, target_resolution, mime, duration, num_frames, if numpy_image is None: - thumb_path = os.path.join( HC.STATIC_DIR, 'hydrus.png' ) - - thumbnail_numpy = HydrusImageHandling.GenerateThumbnailNumPyFromStaticImagePath( thumb_path, target_resolution, HC.IMAGE_PNG ) + thumbnail_numpy = GenerateDefaultThumbnail(mime, target_resolution) else: diff --git a/hydrus/core/files/images/HydrusImageHandling.py b/hydrus/core/files/images/HydrusImageHandling.py index 381fcc5d..b010efa0 100644 --- a/hydrus/core/files/images/HydrusImageHandling.py +++ b/hydrus/core/files/images/HydrusImageHandling.py @@ -9,6 +9,7 @@ import warnings from PIL import ImageFile as PILImageFile from PIL import Image as PILImage +from PIL import ImageOps as PILImageOps try: @@ -663,3 +664,12 @@ def ResizeNumPyImage( numpy_image: numpy.array, target_resolution, forced_interp return cv2.resize( numpy_image, ( target_width, target_height ), interpolation = interpolation ) + +def GenerateDefaultThumbnailNumPyFromPath( path: str, target_resolution: typing.Tuple[ int, int ] ): + + thumb_image = GeneratePILImage( path ) + + pil_image = PILImageOps.pad( thumb_image, target_resolution, PILImage.Resampling.LANCZOS ) + + return GenerateNumPyImageFromPILImage( pil_image, strip_useless_alpha = False ) +