def GenerateThumbnailBytes( path, target_resolution, mime, duration, num_frames, percentage_in = 35 ): if mime in ( HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF, HC.IMAGE_WEBP, HC.IMAGE_TIFF, HC.IMAGE_ICON ): # not apng atm thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( path, target_resolution, mime ) else: if mime == HC.APPLICATION_FLASH: ( os_file_handle, temp_path ) = HydrusPaths.GetTempPath() try: HydrusFlashHandling.RenderPageToFile( path, temp_path, 1 ) thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( temp_path, target_resolution, mime ) except: thumb_path = os.path.join( HC.STATIC_DIR, 'flash.png' ) thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( thumb_path, target_resolution, mime ) finally: HydrusPaths.CleanUpTempPath( os_file_handle, temp_path ) else: renderer = HydrusVideoHandling.VideoRendererFFMPEG( path, mime, duration, num_frames, target_resolution ) renderer.read_frame() # this initialises the renderer and loads the first frame as a fallback desired_thumb_frame = int( ( percentage_in / 100.0 ) * num_frames ) renderer.set_position( desired_thumb_frame ) numpy_image = renderer.read_frame() if numpy_image is None: raise Exception( 'Could not create a thumbnail from that video!' ) numpy_image = HydrusImageHandling.ResizeNumPyImage( numpy_image, target_resolution ) # just in case ffmpeg doesn't deliver right thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesNumPy( numpy_image, mime ) renderer.Stop() del renderer return thumbnail_bytes
def _GetThumbnailHydrusBitmap(self, display_media): bounding_dimensions = self._controller.options['thumbnail_dimensions'] hash = display_media.GetHash() mime = display_media.GetMime() locations_manager = display_media.GetLocationsManager() try: path = self._controller.client_files_manager.GetThumbnailPath( display_media) except HydrusExceptions.FileMissingException as e: if locations_manager.IsLocal(): summary = 'Unable to get thumbnail for file {}.'.format( hash.hex()) self._HandleThumbnailException(e, summary) return self._special_thumbs['hydrus'] try: numpy_image = ClientImageHandling.GenerateNumPyImage(path, mime) except Exception as e: try: # file is malformed, let's force a regen self._controller.files_maintenance_manager.RunJobImmediately( [display_media], ClientFiles.REGENERATE_FILE_DATA_JOB_FORCE_THUMBNAIL, pub_job_key=False) except Exception as e: summary = 'The thumbnail for file {} was not loadable. An attempt to regenerate it failed.'.format( hash.hex()) self._HandleThumbnailException(e, summary) return self._special_thumbs['hydrus'] try: numpy_image = ClientImageHandling.GenerateNumPyImage( path, mime) except Exception as e: summary = 'The thumbnail for file {} was not loadable. It was regenerated, but that file would not render either. Your image libraries or hard drive connection are unreliable. Please inform the hydrus developer what has happened.'.format( hash.hex()) self._HandleThumbnailException(e, summary) return self._special_thumbs['hydrus'] (current_width, current_height) = HydrusImageHandling.GetResolutionNumPy(numpy_image) (media_width, media_height) = display_media.GetResolution() (expected_width, expected_height) = HydrusImageHandling.GetThumbnailResolution( (media_width, media_height), bounding_dimensions) exactly_as_expected = current_width == expected_width and current_height == expected_height rotation_exception = current_width == expected_height and current_height == expected_width correct_size = exactly_as_expected or rotation_exception if not correct_size: it_is_definitely_too_big = current_width >= expected_width and current_height >= expected_height if it_is_definitely_too_big: if HG.file_report_mode: HydrusData.ShowText('Thumbnail {} too big.'.format( hash.hex())) # the thumb we have is larger than desired. we can use it to generate what we actually want without losing significant data # this is _resize_, not _thumbnail_, because we already know the dimensions we want # and in some edge cases, doing getthumbresolution on existing thumb dimensions results in float/int conversion imprecision and you get 90px/91px regen cycles that never get fixed numpy_image = HydrusImageHandling.ResizeNumPyImage( numpy_image, (expected_width, expected_height)) if locations_manager.IsLocal(): # we have the master file, so it is safe to save our resized thumb back to disk since we can regen from source if needed if HG.file_report_mode: HydrusData.ShowText( 'Thumbnail {} too big, saving back to disk.'. format(hash.hex())) try: try: thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesNumPy( numpy_image, mime) except HydrusExceptions.CantRenderWithCVException: thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( path, (expected_width, expected_height), mime) except: summary = 'The thumbnail for file {} was too large, but an attempt to shrink it failed.'.format( hash.hex()) self._HandleThumbnailException(e, summary) return self._special_thumbs['hydrus'] try: self._controller.client_files_manager.AddThumbnailFromBytes( hash, thumbnail_bytes, silent=True) self._controller.files_maintenance_manager.ClearJobs( {hash}, ClientFiles. REGENERATE_FILE_DATA_JOB_REFIT_THUMBNAIL) except: summary = 'The thumbnail for file {} was too large, but an attempt to save back the shrunk file failed.'.format( hash.hex()) self._HandleThumbnailException(e, summary) return self._special_thumbs['hydrus'] else: # the thumb we have is either too small or completely messed up due to a previous ratio misparse media_is_same_size_as_current_thumb = current_width == media_width and current_height == media_height if media_is_same_size_as_current_thumb: # the thumb is smaller than expected, but this is a 32x32 pixilart image or whatever, so no need to scale if HG.file_report_mode: HydrusData.ShowText( 'Thumbnail {} too small due to small source file.'. format(hash.hex())) else: numpy_image = HydrusImageHandling.ResizeNumPyImage( numpy_image, (expected_width, expected_height)) if locations_manager.IsLocal(): # we have the master file, so we should regen the thumb from source if HG.file_report_mode: HydrusData.ShowText( 'Thumbnail {} too small, scheduling regeneration from source.' .format(hash.hex())) delayed_item = display_media.GetMediaResult() with self._lock: if delayed_item not in self._delayed_regeneration_queue_quick: self._delayed_regeneration_queue_quick.add( delayed_item) self._delayed_regeneration_queue.append( delayed_item) else: # we do not have the master file, so we have to scale up from what we have if HG.file_report_mode: HydrusData.ShowText( 'Thumbnail {} was too small, only scaling up due to no local source.' .format(hash.hex())) hydrus_bitmap = ClientRendering.GenerateHydrusBitmapFromNumPyImage( numpy_image) return hydrus_bitmap
def GenerateThumbnailBytes(path, target_resolution, mime, duration, num_frames, percentage_in=35): if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF, HC.IMAGE_WEBP, HC.IMAGE_TIFF, HC.IMAGE_ICON): # not apng atm thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( path, target_resolution, mime) elif mime in [HC.APPLICATION_ZIP]: temp_dir_path = HydrusPaths.GetTempDir() try: cmd = ["unzip", path, '-d', temp_dir_path] subprocess.call(cmd) cover = sorted(list(Path(temp_dir_path).rglob("*.jpg")) + list(Path(temp_dir_path).rglob("*.png")), key=lambda p: p.name)[0].as_posix() thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( cover, target_resolution, mime) except Exception as e: thumb_path = os.path.join(HC.STATIC_DIR, 'zip.png') thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( thumb_path, target_resolution, mime) elif mime in [HC.APPLICATION_RAR]: temp_dir_path = HydrusPaths.GetTempDir() try: cmd = ["unrar", 'x', path, temp_dir_path] subprocess.call(cmd) cover = sorted(list(Path(temp_dir_path).rglob("*.jpg")) + list(Path(temp_dir_path).rglob("*.png")), key=lambda p: p.name)[0].as_posix() thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( cover, target_resolution, mime) except Exception as e: thumb_path = os.path.join(HC.STATIC_DIR, 'rar.png') thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( thumb_path, target_resolution, mime) else: if mime == HC.APPLICATION_FLASH: (os_file_handle, temp_path) = HydrusPaths.GetTempPath() try: HydrusFlashHandling.RenderPageToFile(path, temp_path, 1) thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( temp_path, target_resolution, mime) except: thumb_path = os.path.join(HC.STATIC_DIR, 'flash.png') thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( thumb_path, target_resolution, mime) finally: HydrusPaths.CleanUpTempPath(os_file_handle, temp_path) else: renderer = HydrusVideoHandling.VideoRendererFFMPEG( path, mime, duration, num_frames, target_resolution) renderer.read_frame( ) # this initialises the renderer and loads the first frame as a fallback desired_thumb_frame = int((percentage_in / 100.0) * num_frames) renderer.set_position(desired_thumb_frame) numpy_image = renderer.read_frame() if numpy_image is None: raise Exception( 'Could not create a thumbnail from that video!') numpy_image = HydrusImageHandling.ResizeNumPyImage( numpy_image, target_resolution) # just in case ffmpeg doesn't deliver right thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesNumPy( numpy_image, mime) renderer.Stop() del renderer return thumbnail_bytes