def from_selstr(cls, selstr, name): # TODO this will strip a horizontal line of don't care's lines = [line.strip() for line in selstr.split('\n') if line.strip()] h = len(lines) w = len(lines[0]) lengths = set(len(line) for line in lines) if len(lengths) != 1: raise ValueError("All lines in selstr must be same length") repacked = ''.join(line.strip() for line in lines) buf_selstr = ffi.from_buffer(repacked.encode('ascii')) buf_name = ffi.from_buffer(name.encode('ascii')) sel = lept.selCreateFromString(buf_selstr, h, w, buf_name) return cls(sel)
def frompil(cls, pillow_image): """Create a copy of a PIL.Image from this Pix""" bio = BytesIO() pillow_image.save(bio, format='png', compress_level=1) py_buffer = bio.getbuffer() c_buffer = ffi.from_buffer(py_buffer) with _LeptonicaErrorTrap(): pix = Pix(lept.pixReadMem(c_buffer, len(c_buffer))) return pix
def open(cls, path): """Load an image file into a PIX object. Leptonica can load TIFF, PNM (PBM, PGM, PPM), PNG, and JPEG. If loading fails then the object will wrap a C null pointer. """ with open(path, 'rb') as py_file: data = py_file.read() buffer = ffi.from_buffer(data) with _LeptonicaErrorTrap(): return cls(lept.pixReadMem(buffer, len(buffer)))
def frompil(cls, pillow_image): """Create a copy of a PIL.Image from this Pix""" bio = BytesIO() pillow_image.save(bio, format='png', compress_level=1) py_buffer = bio.getbuffer() if platform.python_implementation() == 'PyPy': # PyPy complains that it cannot do from_buffer(memoryview) py_buffer = bytes(py_buffer) c_buffer = ffi.from_buffer(py_buffer) with _LeptonicaErrorTrap(): pix = Pix(lept.pixReadMem(c_buffer, len(c_buffer))) return pix