Example #1
0
 def _InitialisePIL( self ):
     
     if HG.media_load_report_mode:
         
         HydrusData.ShowText( 'Loading GIF with PIL' )
         
     
     self._cv_mode = False
     
     self._pil_image = HydrusImageHandling.GeneratePILImage( self._path )
     
     self._pil_canvas = None
     
     self._pil_global_palette = self._pil_image.palette
     
     if self._pil_global_palette is not None and False:
         
         self._pil_dirty = self._pil_image.palette.dirty
         self._pil_mode = self._pil_image.palette.mode
         self._pil_rawmode = self._pil_image.palette.rawmode
         
     
     self._next_render_index = 0
     self._last_frame = None
     
     # believe it or not, doing this actually fixed a couple of gifs!
     self._pil_image.seek( 1 )
     self._pil_image.seek( 0 )
Example #2
0
def LoadFromPNG( path ):
    
    # this is to deal with unicode paths, which cv2 can't handle
    ( os_file_handle, temp_path ) = HydrusTemp.GetTempPath()
    
    try:
        
        HydrusPaths.MirrorFile( path, temp_path )
        
        try:
            
            # unchanged because we want exact byte data, no conversions or other gubbins
            numpy_image = cv2.imread( temp_path, flags = IMREAD_UNCHANGED )
            
            if numpy_image is None:
                
                raise Exception()
                
            
        except Exception as e:
            
            try:
                
                # dequantize = False because we don't want to convert to RGB
                
                pil_image = HydrusImageHandling.GeneratePILImage( temp_path, dequantize = False )
                
                numpy_image = HydrusImageHandling.GenerateNumPyImageFromPILImage( pil_image )
                
            except Exception as e:
                
                HydrusData.ShowException( e )
                
                raise Exception( '"{}" did not appear to be a valid image!'.format( path ) )
                
            
        
    finally:
        
        HydrusTemp.CleanUpTempPath( os_file_handle, temp_path )
        
    
    return LoadFromNumPyImage( numpy_image )
Example #3
0
def LoadFromPNG(path):

    # this is to deal with unicode paths, which cv2 can't handle
    (os_file_handle, temp_path) = HydrusTemp.GetTempPath()

    try:

        HydrusPaths.MirrorFile(path, temp_path)

        try:

            # unchanged because we want exact byte data, no conversions or other gubbins
            numpy_image = cv2.imread(temp_path, flags=IMREAD_UNCHANGED)

            if numpy_image is None:

                raise Exception()

        except Exception as e:

            try:

                # dequantize = False because we don't want to convert to RGB

                pil_image = HydrusImageHandling.GeneratePILImage(
                    temp_path, dequantize=False)

                numpy_image = HydrusImageHandling.GenerateNumPyImageFromPILImage(
                    pil_image)

            except Exception as e:

                HydrusData.ShowException(e)

                raise Exception('That did not appear to be a valid image!')

    finally:

        HydrusTemp.CleanUpTempPath(os_file_handle, temp_path)

    try:

        height = numpy_image.shape[0]
        width = numpy_image.shape[1]

        if len(numpy_image.shape) > 2:

            depth = numpy_image.shape[2]

            if depth != 1:

                raise Exception('The file did not appear to be monochrome!')

        try:

            complete_data = numpy_image.tostring()

            top_height_header = complete_data[:2]

            (top_height, ) = struct.unpack('!H', top_height_header)

            payload_and_header_bytes = complete_data[width * top_height:]

        except:

            raise Exception('Header bytes were invalid!')

        try:

            payload_length_header = payload_and_header_bytes[:4]

            (payload_bytes_length, ) = struct.unpack('!I',
                                                     payload_length_header)

            payload_bytes = payload_and_header_bytes[4:4 +
                                                     payload_bytes_length]

        except:

            raise Exception('Payload bytes were invalid!')

    except Exception as e:

        HydrusData.PrintException(e)

        message = 'The image loaded, but it did not seem to be a hydrus serialised png! The error was: {}'.format(
            str(e))
        message += os.linesep * 2
        message += 'If you believe this is a legit non-resized, non-converted hydrus serialised png, please send it to hydrus_dev.'

        raise Exception(message)

    return payload_bytes