ctypes和指针操作 [英] ctypes and pointer manipulation

查看:140
本文介绍了ctypes和指针操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理图像缓冲区,并且我希望能够访问图像中的几行数据以使用c库进行分析。我已经使用create_string_buffer在Python中创建了8位像素缓冲区。有没有一种方法可以在不重新创建新缓冲区的情况下获得指向该缓冲区中某个位置的指针?我的目标是分批分析和更改该缓冲区中的数据,而不必进行大量缓冲区创建和数据复制。

I am dealing with image buffers, and I want to be able to access data a few lines into my image for analysis with a c library. I have created my 8-bit pixel buffer in Python using create_string_buffer. Is there a way to get a pointer to a location within that buffer without re-creating a new buffer? My goal is to analyze and change data within that buffer in chunks, without having to do a lot of buffer creation and data copying.

在这种情况下,最终,C库正在对缓冲区进行所有操作,因此我实际上不必使用Python来更改缓冲区内的值。我只需要让C函数访问缓冲区中的数据即可。

In this case, ultimately, the C library is doing all the manipulation of the buffer, so I don't actually have to change values within the buffer using Python. I just need to give my C function access to data within the buffer.

推荐答案

create_string_buffer 为您提供一个ctypes对象(一个字符数组),然后 byref ,我引用

create_string_buffer gives you a ctypes object (an array of chars), then byref, and I quote,


返回指向obj
的轻量指针,该指针必须是ctypes
类型的实例。 offset默认为零,并且
必须是将
添加到内部指针值的整数。

Returns a light-weight pointer to obj, which must be an instance of a ctypes type. offset defaults to zero, and must be an integer that will be added to the internal pointer value.

在2.6中添加了 offset 参数,因此,如果您使用的是旧版本,那么不幸的是,您需要做更多的工作。

The offset argument has been added in 2.6, so if you're stuck with an older version you will unfortunately need more work.

这篇关于ctypes和指针操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆