How to send file as stream from python to a C library -
i trying use leptonica (c library) within python. library has pixread
method takes absolute path image file parameter. i'm calling python this:
leptonica = ctypes.cdll.loadlibrary("/path/to/lept.so") pix_image = leptonica.pixread("/path/to/image.jgp")
however, call method pixreadstream
that takes in file stream input parameter. in python program, have access image numpy array using opencv.
question
is there way pass image have numpy array in python program pixreadstream
method in leptopnica c library takes file stream input parameter?
roi #opencv image numpy array leptonica = ctypes.cdll.loadlibrary("/path/to/lept.so") pix_image = leptonica.pixreadstream(##any way pass roi here?##)
creating leptonica pix
structure numpy array can done without encoding data image format meant storage , channeling somehow via kernel file within same process. convert array data opencv rgba data , wrap enough leptonica create empty pix
structure of appropriate size , copy data array pix
.
here small example how load image opencv, convert python object wrapping pix
structure, , save image data leptonica file again:
#!/usr/bin/env python # coding: utf8 __future__ import absolute_import, division, print_function ctypes import c_char_p, c_uint32, c_void_p, cdll, memmove, pointer, pointer ctypes.util import find_library import cv2 leptonica = cdll(find_library('lept')) _pix_create = leptonica.pixcreate _pix_create.argtypes = [c_uint32, c_uint32, c_uint32] _pix_create.restype = c_void_p _pix_destroy = leptonica.pixdestroy _pix_destroy.argtypes = [pointer(c_void_p)] _pix_destroy.restype = none _pix_get_data = leptonica.pixgetdata _pix_get_data.argtypes = [c_void_p] _pix_get_data.restype = pointer(c_uint32) _pix_endian_byte_swap = leptonica.pixendianbyteswap _pix_endian_byte_swap.argtypes = [c_void_p] _pix_endian_byte_swap.restype = c_uint32 _pix_write_implied_format = leptonica.pixwriteimpliedformat _pix_write_implied_format.argtypes = [c_char_p, c_void_p, c_uint32, c_uint32] _pix_write_implied_format.restype = c_uint32 class pix(object): def __init__(self, width, height, depth): self._as_parameter_ = _pix_create(width, height, depth) self._pointer = pointer self._pix_destroy = _pix_destroy def __del__(self): pix_pointer = self._pointer(c_void_p(self._as_parameter_)) self._pix_destroy(pix_pointer) assert pix_pointer[0] none @property def data(self): return _pix_get_data(self) def endian_byte_swap(self): _pix_endian_byte_swap(self) def save(self, filename, quality=0, progessive=false): _pix_write_implied_format(filename, self, quality, progessive) @classmethod def from_rgba(cls, array): width, height, depth = array.shape if depth != 4 , array.itemsize != 1: raise valueerror('array has wrong format') result = cls(width, height, 32) memmove(result.data, array.ctypes.data, array.size * array.itemsize) result.endian_byte_swap() return result def main(): image = cv2.imread('test.jpg') image = cv2.cvtcolor(image, cv2.cv.cv_bgr2rgba) pix = pix.from_rgba(image) pix.save('test.png') if __name__ == '__main__': main()
Comments
Post a Comment