コード例 #1
0
ファイル: mediafile.py プロジェクト: puzzlet/cgkit
    def numpyArray(self, pixelFormat=RGB, pixelAccess=WIDTH_HEIGHT, colorAccess=SEPARATE_CHANNELS):
        """Return the current frame as a numpy array.

        pixelFormat specifies the number and order of the channels in the
        returned buffer. The source video frames are converted into the
        specified format.
        
        pixelAccess defines the order of the x,y indices when accessing
        a pixel in the returned buffer. The value can either be WIDTH_HEIGHT
        to specify the pixel position as (x,y) or HEIGHT_WIDTH if (y,x)
        is required.
        
        colorAccess specifies whether the color channels should be accessed
        as a third index (SEPARATE_CHANNELS) or if the entire pixel should
        be stored as a single integer value (COMBINED_CHANNELS). The latter
        is only allowed if the pixel format contains 1 or 4 channels.
        
        Note that the pixelAccess and colorAccess parameters do not affect
        the underlying memory layout of the buffer, they only affect the way
        that individual pixels are accessed via the numpy array.
        The memory layout is always so that rows are stored one after another
        without any gaps and the channel values of an individual pixel are
        always stored next to each other.  
        
        The underlying numpy array is reused for every frame, so if you need
        to keep the array you have to copy it.
        The returned array has a shape of (width,height,3) and a dtype of uint8.
        """
        # Make sure numpy has been imported successfully
        global _numpyImportException
        if _numpyImportException is not None:
            raise _numpyImportException
        
        # Do we need to allocate a new numpy buffer?
        if self._numpyArray is None or (pixelFormat, pixelAccess, colorAccess)!=self._numpyBufferParams:
            self._initNumpyProcessing(pixelFormat, pixelAccess, colorAccess)

        # Convert the frame into an rgb image. The result is put directly into
        # the numpy array.
        height = self.size[1]
        swscale.sws_scale(self._numpySwsCtx, self._frame.data, self._frame.linesize, 0, height, self._dataPtrs, self._lineSizes)
        
        return self._numpyArray
コード例 #2
0
ファイル: mediafile.py プロジェクト: puzzlet/cgkit
    def pilImage(self):
        """Return the current frame as a PIL image.
        """
        global _pilImportException
        if _pilImportException is not None:
            raise _pilImportException

        # Do we need to initialize the PIL conversion?
        if self._pilSwsCtx is None:
            self._initPilProcessing()

        # Convert the image into RGB format (the convered image is stored
        # in the AVPicture buffer)...
        height = self.size[1]
        swscale.sws_scale(self._pilSwsCtx, self._frame.data, self._frame.linesize, 0, height, self._picture.data, self._picture.linesize)
            
        # Obtain a Python string containing the RGB image data...
        dataStr = ctypes.string_at(self._picture.data[0], self._pictureSize)
        
        # Convert to PIL image
        return Image.fromstring("RGB", self.size, dataStr)