Numpy 2D-缓冲区中的数组?

2024-01-05

我有一个内存映射,其中包含一个 2D 数组,我想从中创建一个 numpy 数组。理想情况下,我想避免复制,因为涉及的数组可能很大。

我的代码如下所示:

n_bytes = 10000
tagname = "Some Tag from external System"
map = mmap.mmap(-1, n_bytes, tagname)
offsets = [0, 5000]

columns = []
for offset in offsets:
   #type and count vary in the real code, but for this dummy code I simply made them up. But I know the count and type for every column.
   np_type = np.dtype('f4')
   column_data = np.frombuffer(map, np_type, count=500, offset=offset)
   columns.append(column_data)

# this line seems to copy the data, which I would like to avoid
data = np.array(columns).T

假设您有一个字节数组并且您知道它的尺寸,那么答案非常简单。 想象一下,您将图像的原始 RGB 数据(每像素 24 位)存储在缓冲区(名为“buff”)中 尺寸为 1024x768

#read the buffer into 1D byte array
arr = numpy.frombuffer(buff, dtype=numpy.uint8)
#now shape the array as you please
arr.shape = (768,1024,3)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Numpy 2D-缓冲区中的数组? 的相关文章

随机推荐