def __repr__(self): if self._cdata: s = "<leptonica.Pix image size={0}x{1} depth={2}{4} at 0x{3:x}>" return s.format( self._cdata.w, self._cdata.h, self._cdata.d, int(ffi.cast('intptr_t', self._cdata)), '(colormapped)' if self._cdata.colormap else '', ) else: return "<leptonica.Pix image NULL>"
def _repr_png_(self): """iPython display hook returns png version of image """ data = ffi.new('l_uint8 **') size = ffi.new('size_t *') err = lept.pixWriteMemPng(data, size, self._cdata, 0) if err != 0: raise LeptonicaIOError("pixWriteMemPng") char_data = ffi.cast('char *', data[0]) return ffi.buffer(char_data, size[0])[:]
def __getstate__(self): data = ffi.new('l_uint32 **') size = ffi.new('size_t *') err = lept.pixSerializeToMemory(self._cdata, data, size) if err != 0: raise LeptonicaIOError("pixSerializeToMemory") char_data = ffi.cast('char *', data[0]) # Copy from C bytes to python bytes() data_bytes = ffi.buffer(char_data, size[0])[:] # Can now free C bytes lept.lept_free(char_data) return dict(data=data_bytes)
def __setstate__(self, state): cdata_bytes = ffi.new('char[]', state['data']) cdata_uint32 = ffi.cast('l_uint32 *', cdata_bytes) pix = lept.pixDeserializeFromMemory(cdata_uint32, len(state['data'])) Pix.__init__(self, pix)