def from_buffer(buf): ptr = ffi.new('WebPData*') lib.WebPDataInit(ptr) data_ref = ffi.from_buffer(buf) ptr.size = len(buf) ptr.bytes = ffi.cast('uint8_t*', data_ref) return WebPData(ptr, data_ref)
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)
def new(use_threads=False, color_mode=WebPColorMode.RGBA): ptr = ffi.new('WebPAnimDecoderOptions*') if lib.WebPAnimDecoderOptionsInit(ptr) == 0: raise WebPError('version mismatch') dec_opts = WebPAnimDecoderOptions(ptr) dec_opts.use_threads = use_threads dec_opts.color_mode = color_mode return dec_opts
def new(minimize_size=False, allow_mixed=False): ptr = ffi.new('WebPAnimEncoderOptions*') if lib.WebPAnimEncoderOptionsInit(ptr) == 0: raise WebPError('version mismatch') enc_opts = WebPAnimEncoderOptions(ptr) enc_opts.minimize_size = minimize_size enc_opts.allow_mixed = allow_mixed return enc_opts
def new(preset=WebPPreset.DEFAULT, quality=75, lossless=False): ptr = ffi.new('WebPConfig*') if lib.WebPConfigPreset(ptr, preset.value, quality) == 0: raise WebPError('failed to load config from preset') config = WebPConfig(ptr) config.lossless = lossless if not config.validate(): raise WebPError('config is not valid') return config
def decode_frame(self): """Decodes the next frame of the animation. Returns: numpy.array: The frame image. float: The timestamp for the end of the frame. """ timestamp_ptr = ffi.new('int*') buf_ptr = ffi.new('uint8_t**') if lib.WebPAnimDecoderGetNext(self.ptr, buf_ptr, timestamp_ptr) == 0: raise WebPError('decoding error') size = self.anim_info.height * self.anim_info.width * 4 buf = ffi.buffer(buf_ptr[0], size) arr = np.copy(np.frombuffer(buf, dtype=np.uint8)) arr = np.reshape(arr, (self.anim_info.height, self.anim_info.width, 4)) # timestamp_ms contains the _end_ time of this frame timestamp_ms = timestamp_ptr[0] return arr, timestamp_ms
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)
def new(preset=WebPPreset.DEFAULT, quality=75, lossless=False): """Create a new WebPConfig instance to describe encoder settings. Args: preset (WebPPreset): quality (int): Quality (0-100, where 0 is lowest quality). lossless (bool): Set to True for lossless compression. Returns: WebPConfig: The new WebPConfig instance. """ ptr = ffi.new('WebPConfig*') if lib.WebPConfigPreset(ptr, preset.value, quality) == 0: raise WebPError('failed to load config from preset') config = WebPConfig(ptr) config.lossless = lossless if not config.validate(): raise WebPError('config is not valid') return config
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)
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)
def new(): ptr = ffi.new('WebPAnimInfo*') return WebPAnimInfo(ptr)
def new(): ptr = ffi.new('WebPDecoderConfig*') if lib.WebPInitDecoderConfig(ptr) == 0: raise WebPError('failed to init decoder config') return WebPDecoderConfig(ptr)
def new(): ptr = ffi.new('WebPMemoryWriter*') lib.WebPMemoryWriterInit(ptr) return WebPMemoryWriter(ptr)
def __init__(self): self.ptr = ffi.new('WebPData*') lib.WebPDataInit(self.ptr)
def __init__(self, ptr=None): if ptr is None: ptr = ffi.new('WebPConfig*') lib.WebPConfigInit(ptr) self.ptr = ptr