Пример #1
0
 def _GetCurrentFrame( self ):
     
     if self._cv_mode:
         
         ( retval, numpy_image ) = self._cv_video.read()
         
         if not retval:
             
             self._next_render_index = ( self._next_render_index + 1 ) % self._num_frames
             
             raise HydrusExceptions.CantRenderWithCVException( 'CV could not render frame ' + str( self._next_render_index - 1 ) + '.' )
             
         
     else:
         
         current_frame = HydrusImageHandling.Dequantize( self._pil_image )
         
         if current_frame.mode == 'RGBA':
             
             if self._pil_canvas is None:
                 
                 self._pil_canvas = current_frame
                 
             else:
                 
                 self._pil_canvas.paste( current_frame, None, current_frame ) # use the rgba image as its own mask
                 
             
         elif current_frame.mode == 'RGB':
             
             self._pil_canvas = current_frame
             
         
         numpy_image = HydrusImageHandling.GenerateNumPyImageFromPILImage( self._pil_canvas )
         
     
     self._next_render_index = ( self._next_render_index + 1 ) % self._num_frames
     
     if self._next_render_index == 0:
         
         self._RewindGIF()
         
     else:
         
         if not self._cv_mode:
             
             self._pil_image.seek( self._next_render_index )
             
             if self._pil_global_palette is not None and self._pil_image.palette == self._pil_global_palette: # for some reason, when pil falls back from local palette to global palette, a bunch of important variables reset!
                 
                 self._pil_image.palette.dirty = self._pil_dirty
                 self._pil_image.palette.mode = self._pil_mode
                 self._pil_image.palette.rawmode = self._pil_rawmode
                 
             
         
     
     return numpy_image
Пример #2
0
def GenerateThumbnailBytesNumPy(numpy_image, mime) -> bytes:

    (im_height, im_width, depth) = numpy_image.shape

    if depth == 4:

        if NumPyImageHasOpaqueAlphaChannel(numpy_image):

            convert = cv2.COLOR_RGBA2BGR

        else:

            convert = cv2.COLOR_RGBA2BGRA

    else:

        convert = cv2.COLOR_RGB2BGR

    numpy_image = cv2.cvtColor(numpy_image, convert)

    (im_height, im_width, depth) = numpy_image.shape

    if depth == 4:

        ext = '.png'

        params = CV_PNG_THUMBNAIL_ENCODE_PARAMS

    else:

        ext = '.jpg'

        params = CV_JPEG_THUMBNAIL_ENCODE_PARAMS

    (result_success, result_byte_array) = cv2.imencode(ext, numpy_image,
                                                       params)

    if result_success:

        thumbnail_bytes = result_byte_array.tostring()

        return thumbnail_bytes

    else:

        raise HydrusExceptions.CantRenderWithCVException(
            'Thumb failed to encode!')
Пример #3
0
def GenerateThumbnailBytesNumPy( numpy_image, mime ):
    
    if not OPENCV_OK:
        
        pil_image = GeneratePILImageFromNumPyImage( numpy_image )
        
        return GenerateThumbnailBytesPIL( pil_image, mime )
        
    
    ( im_y, im_x, depth ) = numpy_image.shape
    
    if depth == 4:
        
        convert = cv2.COLOR_RGBA2BGRA
        
    else:
        
        convert = cv2.COLOR_RGB2BGR
        
    
    numpy_image = cv2.cvtColor( numpy_image, convert )
    
    if mime == HC.IMAGE_JPEG:
        
        ext = '.jpg'
        
        params = CV_JPEG_THUMBNAIL_ENCODE_PARAMS
        
    else:
        
        ext = '.png'
        
        params = CV_PNG_THUMBNAIL_ENCODE_PARAMS
        
    
    ( result_success, result_byte_array ) = cv2.imencode( ext, numpy_image, params )
    
    if result_success:
        
        thumbnail_bytes = result_byte_array.tostring()
        
        return thumbnail_bytes
        
    else:
        
        raise HydrusExceptions.CantRenderWithCVException( 'Thumb failed to encode!' )