- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 Python、PIL 和 ctypes 进行图像处理。当我一起破解东西时,我使用 PIL 的 fromstring
函数将像素缓冲区从 ctypes 获取到 PIL 对象中。我只是遍历数组,构建 python 字符串。
这行得通
tx = foo.tx
tx.restype = POINTER(c_ubyte)
result = tx(...args...)
#TODO there must also be a better way to do this
pystr = ""
for i in xrange(w*h*4):
pystr += result[i]
i = Image.fromstring("RGBA", (w, h), pystr)
i.save("out.png")
它不是很漂亮,但是很管用。用 TODO 评论并继续。在初始管道就位后,分析显示此代码块存在严重的性能问题。我想这并不奇怪。
这不起作用
类似于这个问题:Pixel manipulation with PIL.Image and ctypes ,我正在尝试使用 frombuffer()
将像素数据更有效地获取到 PIL 对象中:
tx = foo.tx
tx.restype = POINTER(c_ubyte)
result = tx(...args...)
i = Image.frombuffer('RGBA', (w, h), result, 'raw', 'RGBA', 0, 1)
i.save("out.png")
尽管 fromstring
有效,但使用 frombuffer
会导致以下异常:
Traceback (most recent call last):
File "test.py", line 53, in <module>
i = Image.frombuffer('RGBA', (w, h), res, 'raw', 'RGBA', 0, 1)
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 1853, in frombuffer
core.map_buffer(data, size, decoder_name, None, 0, args)
ValueError: buffer is not large enough
环境
缓冲区在 C 中被 malloc
为:
unsigned char *pixels_color = (unsigned char*)malloc((WIDTH*HEIGHT*4)*sizeof(unsigned char*));
编辑
基于 eryksun在下面的评论和回答中,我将缓冲区分配从 C 库中的 malloc
移到了 Python 中,并将指针传递给了 C:
tx = foo.tx
tx.restype = POINTER(c_ubyte)
pix_clr = (c_ubyte*(w*h*4))()
tx(...args..., pix_clr)
i = Image.frombuffer('RGBA', (w, h), pix_clr, 'raw', 'RGBA', 0, 1)
i.save("out.png")
这按预期工作。它仍然没有解释 为什么 PIL 对 C 分配的缓冲区不满意,但无论如何这是更好的内存管理结构。感谢 ErykSun 和 HYRY!
最佳答案
当您设置 tx.restype = POINTER(c_ubyte)
时,生成的 ctypes 指针对象是一个 4 或 8 字节的缓冲区,用于图像缓冲区的地址 .您需要从该地址创建一个 ctypes 数组。
如果你提前知道大小,你可以设置SIZE = WIDTH * HEIGHT * 4; tx.restype = POINTER(c_ubyte * SIZE)
。否则设置 tx.restype = POINTER(c_ubyte)
,并转换为动态大小,即 size = w * h * 4; pbuf = cast(result, POINTER(c_ubyte * size))
.无论哪种方式,您都需要取消引用指针以获取数组。使用 buf = pbuf[0]
或 buf = pbuf.contents
。然后你可以将 buf
传递给 Image.frombuffer
。
也就是说,使用 ctypes 分配内存通常更简单。这为您提供了引用计数内存管理,而不必手动释放内存。以下是一个玩具示例。
假设是动态大小,调用者需要获取图像尺寸以便分配数组,所以我添加了一个结构,它具有图像的宽度、高度和深度,由 C 填充函数 get_image_info
。函数 get_image
接收指向图像数组的指针以复制数据。
import ctypes
lib = ctypes.CDLL('imgtest.dll')
class ImageInfo(ctypes.Structure):
_fields_ = (
('width', ctypes.c_int),
('height', ctypes.c_int),
('depth', ctypes.c_int),
)
pImageInfo = ctypes.POINTER(ImageInfo)
pImage = ctypes.POINTER(ctypes.c_ubyte)
lib.get_image_info.argtypes = [pImageInfo]
lib.get_image_info.restype = ctypes.c_int
lib.get_image.argtypes = [pImage]
lib.get_image.restype = ctypes.c_int
imgnfo = ImageInfo()
lib.get_image_info(ctypes.byref(imgnfo))
w, h, d = imgnfo.width, imgnfo.height, imgnfo.depth
imgdata = (w * h * d * ctypes.c_ubyte)()
lib.get_image(imgdata)
from PIL import Image
img = Image.frombuffer('RGBA', (w, h), imgdata, 'raw', 'RGBA', 0, 1)
C 声明:
typedef struct _ImageInfo {
int width;
int height;
int depth;
} ImageInfo, *pImageInfo;
typedef unsigned char *pImage;
int get_image_info(pImageInfo imgnfo);
int get_image(pImage img);
关于python - 使用 ctypes 数组时 PIL 的 Image.frombuffer 预期数据长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7142169/
我似乎找不到任何将 ctypes.c_void_p() 转换为字符串或字节数组的简单示例。有没有简单的衬里可以做到这一点? 最佳答案 给你: import ctypes as ct # set up
在ctypes中,pointer和byref有什么区别?它们似乎都是将指针传递给函数的一种方式,例如作为输出参数。 最佳答案 在功能上,它们是等价的。 然而,python docs请指出 pointe
我知道我应该指定 argtypes对于我的 C/C++ 函数,因为我的某些调用会导致堆栈损坏。 myCfunc.argtypes = [ct.c_void_p, ct.POINTER(ct.c
有没有办法获取指向 ctypes 数组中间元素的指针?示例: lib = ctypes.cdll.LoadLibrary('./lib.so') arr = (ctypes.c_int32 * 100
在我自定义的 TYPO3 Extbase 扩展中,我创建了一个后端模块来管理个人记录。现在我需要一个内容元素来在前端显示记录。 我看到了两种实现此目的的方法: 使用 CType“list”和自定义 l
实际上,我正在尝试将 ctypes 数组转换为 python 列表并返回。 如果找到this thread 。但它假设我们在编译时知道类型。 但是是否可以检索元素的 ctypes 类型? 我有一个 p
我正在将 float 列表转换为具有以下字段的 ctypes Structure 类,然后再将它们传递给 FFI 函数: FFIArray(Structure): _fields_ = [("
我需要将异质数据的二维数组从我的 c dll 返回到 python。 为此目的,我从我的 c dll 返回一个元组的元组。它作为 PyObject 返回 * 这个元组的元组需要作为第一行第一列的 tu
这是不一致的: from ctypes import * class S(Structure): _fields_ = [("x", POINTER(c_int)), ("y", c_int)
我真的希望一些 Python/Ctypes/C 专家可以帮助我解决这个问题,这可能是我在使用 Python 与 C 库交互时正确使用 Ctypes 的类型结构方面缺乏知识。 目标:我需要访问几个使用
我正在尝试调试 python 使用 ctypes 调用 C 函数的代码。我感兴趣的 python 代码中的一行看起来像: returnValue = cfunction() 其中 cfunction
我正在开发 DLL/SO 的 Python 包装器。我已经验证了代码可以调用实际的 DLL 和 SO。我想对我的包装器进行单元测试,而不需要安装底层 DLL/SO。我正在考虑使用 mock 。 我遇到
大家。我在使用 ctypes 和 C 代码时遇到内存分配错误。我想知道内存问题是在 C 内部,还是由 ctypes 使用不当引起的。内存错误是 python(79698) malloc: * erro
我想制作一个笑话程序,首先它打开一个消息框,关闭后另一个消息框出现在随机位置。它会一直这样重复,直到有什么东西终止了它的任务。使用 tkinter 消息框,那么这些消息框就无法被 Hook ,我必须制
我对 python 中的变量大小有疑问,我使用 Ctypes 因为我想要一个 1 字节的数字,但是当我试图在 python 中检查它的大小时(通过 sys.getsize ) 它说它是 80 字节但是
我正在尝试在 python lambda 函数中使用 matplotlib 生成图形。我使用库 mathplotlib 导入了一个图层,但它不起作用。 这个想法是生成一个图形,将其保存为临时文件并上传
我正在尝试使用 C 中的 python ctypes 制作简单的库 blake 哈希函数包装器。但只是为了首先测试我的简单 C 辅助函数是否能正常工作,我编写了小的 python 脚本 blake 哈
图书馆代码(简化版): // package1.go package package1 import "C" func Play(s *C.char) { } 客户代码: // main.go pac
到目前为止,我已经得到了一个不适用于 python 的 DLL,并输入 return: I just can't pass it arguments because I doing it wrong
我有一个具有以下签名的 C 函数: void init(int* argc, char** argv[]); 我想使用 Ctypes 从我的 OCaml 代码中调用此函数,但我想不出一个正确的方法来传
我是一名优秀的程序员,十分优秀!