gpt4 book ai didi

python - 如何将文件作为流从 python 发送到 C 库

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:34 25 4
gpt4 key购买 nike

我正在尝试在 python 中使用 Leptonica(C 库)。该库有一个 pixRead 方法,该方法将图像文件的绝对路径作为参数。目前我已经成功地从 python 中调用它:

leptonica = ctypes.cdll.LoadLibrary("/path/to/lept.so")
pix_image = leptonica.pixRead("/path/to/image.jgp")

但是,我想调用方法pixReadStream that takes in file stream as an input parameter .在我的 python 程序中,我可以使用 OpenCV 将图像作为 numpy 数组访问。

问题

有什么方法可以将我在 Python 程序中作为 numpy 数组的图像传递给 leptopnica C 库中的 pixReadStream 方法,该方法将文件流作为输入参数?

roi #OpenCV image numpy array
leptonica = ctypes.cdll.LoadLibrary("/path/to/lept.so")
pix_image = leptonica.pixReadStream(##any way to pass roi here?##)

最佳答案

从 Numpy 数组创建 Leptonica PIX 结构可以在不将数据编码为图像格式的情况下完成,图像格式用于存储和以某种方式通过内核作为同一进程中的文件传输。将数组数据从 OpenCV 转换为 RGBA 数据,并从 Leptonica 进行足够的包装以创建适当大小的空 PIX 结构,然后将数据从数组复制到 PIX

这是一个小例子,如何使用 OpenCV 加载图像,将其转换为包装 PIX 结构的 Python 对象,并再次使用 Leptonica 将图像数据保存到文件中:

#!/usr/bin/env python
# coding: utf8
from __future__ import absolute_import, division, print_function
from ctypes import c_char_p, c_uint32, c_void_p, CDLL, memmove, pointer, POINTER
from 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] is 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 and 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()

关于python - 如何将文件作为流从 python 发送到 C 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36709718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com