Exemplo n.º 1
0
    def from_numpy(arr, **_3to2kwargs):
        if 'pilmode' in _3to2kwargs: pilmode = _3to2kwargs['pilmode']; del _3to2kwargs['pilmode']
        else: pilmode = None
        ptr = ffi.new('WebPPicture*')
        if lib.WebPPictureInit(ptr) == 0:
            raise WebPError('version mismatch')
        ptr.height, ptr.width, bytes_per_pixel = arr.shape

        if pilmode is None:
            if bytes_per_pixel == 3:
                import_func = lib.WebPPictureImportRGB
            elif bytes_per_pixel == 4:
                import_func = lib.WebPPictureImportRGBA
            else:
                raise WebPError('cannot infer color mode from array of shape ' + repr(arr.shape))
        else:
            if pilmode == 'RGB':
                import_func = lib.WebPPictureImportRGB
            elif pilmode == 'RGBA':
                import_func = lib.WebPPictureImportRGBA
            else:
                raise WebPError('unsupported image mode: ' + pilmode)

        pixels = ffi.cast('uint8_t*', ffi.from_buffer(arr))
        stride = ptr.width * bytes_per_pixel
        ptr.use_argb = 1
        if import_func(ptr, pixels, stride) == 0:
            raise WebPError('memory error')
        return WebPPicture(ptr)
Exemplo n.º 2
0
 def new(width, height):
     ptr = ffi.new('WebPPicture*')
     if lib.WebPPictureInit(ptr) == 0:
         raise WebPError('version mismatch')
     ptr.width = width
     ptr.height = height
     if lib.WebPPictureAlloc(ptr) == 0:
         raise WebPError('memory error')
     return WebPPicture(ptr)
Exemplo n.º 3
0
    def from_pil(img):
        ptr = ffi.new('WebPPicture*')
        if lib.WebPPictureInit(ptr) == 0:
            raise WebPError('version mismatch')
        ptr.width = img.width
        ptr.height = img.height

        if img.mode == 'RGB':
            import_func = lib.WebPPictureImportRGB
            bytes_per_pixel = 3
        elif img.mode == 'RGBA':
            import_func = lib.WebPPictureImportRGBA
            bytes_per_pixel = 4
        else:
            raise WebPError('unsupported image mode: ' + img.mode)

        arr = np.asarray(img, dtype=np.uint8)
        pixels = ffi.cast('uint8_t*', ffi.from_buffer(arr))
        stride = img.width * bytes_per_pixel
        ptr.use_argb = 1
        if import_func(ptr, pixels, stride) == 0:
            raise WebPError('memory error')
        return WebPPicture(ptr)
Exemplo n.º 4
0
    def from_numpy(arr, *, pilmode=None):
        ptr = ffi.new('WebPPicture*')
        if lib.WebPPictureInit(ptr) == 0:
            raise WebPError('version mismatch')

        if len(arr.shape) == 3:
            bytes_per_pixel = arr.shape[-1]
        elif len(arr.shape) == 2:
            bytes_per_pixel = 1
        else:
            raise WebPError('unexpected array shape: ' + repr(arr.shape))

        if pilmode is None:
            if bytes_per_pixel == 3:
                import_func = lib.WebPPictureImportRGB
            elif bytes_per_pixel == 4:
                import_func = lib.WebPPictureImportRGBA
            else:
                raise WebPError(
                    'cannot infer color mode from array of shape ' +
                    repr(arr.shape))
        else:
            if pilmode == 'RGB':
                import_func = lib.WebPPictureImportRGB
            elif pilmode == 'RGBA':
                import_func = lib.WebPPictureImportRGBA
            else:
                raise WebPError('unsupported image mode: ' + pilmode)

        ptr.height, ptr.width = arr.shape[:2]
        pixels = ffi.cast('uint8_t*', ffi.from_buffer(arr))
        stride = ptr.width * bytes_per_pixel
        ptr.use_argb = 1
        if import_func(ptr, pixels, stride) == 0:
            raise WebPError('memory error')
        return WebPPicture(ptr)