Exemple #1
0
def GenerateThumbnail( path, dimensions = HC.UNSCALED_THUMBNAIL_DIMENSIONS ):
    
    mime = GetMime( path )
    
    f = cStringIO.StringIO()
    
    if mime in HC.IMAGES:
        
        pil_image = HydrusImageHandling.GeneratePILImage( path )
        
        SaveThumbnailToStream( pil_image, dimensions, f )
        
    elif mime == HC.APPLICATION_FLASH:
        
        ( os_file_handle, temp_path ) = HydrusPaths.GetTempPath()
        
        try:
            
            HydrusFlashHandling.RenderPageToFile( path, temp_path, 1 )
            
            pil_image = HydrusImageHandling.GeneratePILImage( temp_path )
            
            SaveThumbnailToStream( pil_image, dimensions, f )
            
        except:
            
            flash_default_path = os.path.join( HC.STATIC_DIR, 'flash.png' )
            
            pil_image = HydrusImageHandling.GeneratePILImage( flash_default_path )
            
            SaveThumbnailToStream( pil_image, dimensions, f )
            
        finally:
            
            del pil_image
            
            HydrusPaths.CleanUpTempPath( os_file_handle, temp_path )
            
        
    else:
        
        ( size, mime, width, height, duration, num_frames, num_words ) = GetFileInfo( path )
        
        cropped_dimensions = HydrusImageHandling.GetThumbnailResolution( ( width, height ), dimensions )
        
        renderer = HydrusVideoHandling.VideoRendererFFMPEG( path, mime, duration, num_frames, cropped_dimensions )
        
        numpy_image = renderer.read_frame()
        
        if numpy_image is None:
            
            raise Exception( 'Could not create a thumbnail from that video!' )
            
        
        pil_image = HydrusImageHandling.GeneratePILImageFromNumpyImage( numpy_image )
        
        SaveThumbnailToStream( pil_image, dimensions, f )
        
    
    f.seek( 0 )
    
    thumbnail = f.read()
    
    f.close()
    
    return thumbnail
Exemple #2
0
def GenerateThumbnail(path,
                      mime,
                      dimensions=HC.UNSCALED_THUMBNAIL_DIMENSIONS,
                      percentage_in=35):

    if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF):

        thumbnail = GenerateThumbnailFromStaticImage(path, dimensions, mime)

    else:

        f = cStringIO.StringIO()

        if mime == HC.APPLICATION_FLASH:

            (os_file_handle, temp_path) = HydrusPaths.GetTempPath()

            try:

                HydrusFlashHandling.RenderPageToFile(path, temp_path, 1)

                pil_image = HydrusImageHandling.GeneratePILImage(temp_path)

                SaveThumbnailToStreamPIL(pil_image, dimensions, f)

            except:

                flash_default_path = os.path.join(HC.STATIC_DIR, 'flash.png')

                pil_image = HydrusImageHandling.GeneratePILImage(
                    flash_default_path)

                SaveThumbnailToStreamPIL(pil_image, dimensions, f)

            finally:

                del pil_image

                HydrusPaths.CleanUpTempPath(os_file_handle, temp_path)

        else:

            (size, mime, width, height, duration, num_frames,
             num_words) = GetFileInfo(path)

            cropped_dimensions = HydrusImageHandling.GetThumbnailResolution(
                (width, height), dimensions)

            renderer = HydrusVideoHandling.VideoRendererFFMPEG(
                path, mime, duration, num_frames, cropped_dimensions)

            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!')

            pil_image = HydrusImageHandling.GeneratePILImageFromNumpyImage(
                numpy_image)

            SaveThumbnailToStreamPIL(pil_image, dimensions, f)

        f.seek(0)

        thumbnail = f.read()

        f.close()

    return thumbnail