Esempio n. 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 = pil_image = 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 = ClientImageHandling.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
Esempio n. 2
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:

            if self._pil_image.mode == 'P' and 'transparency' in self._pil_image.info:

                # The gif problems seem to be here.
                # I think that while some transparent animated gifs expect their frames to be pasted over each other, the others expect them to be fresh every time.
                # Determining which is which doesn't seem to be available in PIL, and PIL's internal calculations seem to not be 100% correct.
                # Just letting PIL try to do it on its own with P rather than converting to RGBA sometimes produces artifacts

                current_frame = self._pil_image.convert('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

            else:
                self._pil_canvas = self._pil_image

            numpy_image = ClientImageHandling.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
Esempio n. 3
0
def GetVideoFrameDuration( path ):

    cv_video = cv2.VideoCapture( path )
    
    fps = cv_video.get( CAP_PROP_FPS )
    
    if fps in ( 0, 1000 ): raise HydrusExceptions.CantRenderWithCVException()
    
    return 1000.0 / fps
Esempio n. 4
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 ' + HC.u( self._next_render_index - 1 ) + '.' )
             
         
     else:
         
         if self._pil_image.mode == 'P' and 'transparency' in self._pil_image.info:
             
             # I think gif problems are around here somewhere; the transparency info is not converted to RGBA properly, so it starts drawing colours when it should draw nothing
             
             current_frame = self._pil_image.convert( '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
             
         else: self._pil_canvas = self._pil_image
         
         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_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!
                 
                 pil_image.palette.dirty = self._pil_dirty
                 pil_image.palette.mode = self._pil_mode
                 pil_image.palette.rawmode = self._pil_rawmode
                 
             
         
     
     return numpy_image