如何将双指针 c 类型转换为 numpy 数组?
How can i cast a double pointer ctype to numpy array?
嘿,我得到了这段代码,我用它来将 numpy 二维数组转换为 ctype 双指针。
import ctypes as ct
import numpy as np
arr = np.empty([500, 500], dtype=np.uint8)
UI8Ptr = ct.POINTER(ct.c_uint8)
UI8PtrPtr = ct.POINTER(UI8Ptr)
ct_arr = np.ctypeslib.as_ctypes(arr)
UI8PtrArr = UI8Ptr * ct_arr._length_
ct_ptr = ct.cast(UI8PtrArr(*(ct.cast(row, UI8Ptr) for row in ct_arr)), UI8PtrPtr)
如何将 ct_ptr 转换回 numpy 二维数组?
注:这是对.
的跟进
一种方式是经历以下状态:
- CTypes指针
- Python 列出
- NumPy 数组
2 个观察结果自动出现:
- 效率很低,但不幸的是我不知道如何摆脱 #2.(这是罪魁祸首)
- 指针包含有关基类型的信息,但不包含创建它的数组的维数,因此您必须 "save" 它们,因为当 "deconstructing"指针
code00.py:
#!/usr/bin/env python3
import sys
import ctypes as ct
import numpy as np
def main():
dim0 = 3
dim1 = 4
arr0 = np.empty([dim0, dim1], dtype=np.uint8)
print("Initial array:\n{0:}".format(arr0))
UI8Ptr = ct.POINTER(ct.c_uint8)
UI8PtrPtr = ct.POINTER(UI8Ptr)
ct_arr = np.ctypeslib.as_ctypes(arr0)
UI8PtrArr = UI8Ptr * ct_arr._length_
ct_ptr = ct.cast(UI8PtrArr(*(ct.cast(row, UI8Ptr) for row in ct_arr)), UI8PtrPtr)
arr1 = np.array([[ct_ptr[i][j] for j in range(dim1)] for i in range(dim0)], dtype=ct_ptr._type_._type_._type_)
print("\nFinal array:\n{0:}".format(arr1))
print("\nEqual arrays: {0:}".format(np.array_equal(arr0, arr1)))
if __name__ == "__main__":
print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
main()
print("\nDone.")
输出:
[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q058781199]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code00.py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32
Initial array:
[[ 96 100 101 115]
[105 114 101 100]
[ 96 96 32 115]]
Final array:
[[ 96 100 101 115]
[105 114 101 100]
[ 96 96 32 115]]
Equal arrays: True
Done.
有关此主题的更多信息,请查看 。
嘿,我得到了这段代码,我用它来将 numpy 二维数组转换为 ctype 双指针。
import ctypes as ct
import numpy as np
arr = np.empty([500, 500], dtype=np.uint8)
UI8Ptr = ct.POINTER(ct.c_uint8)
UI8PtrPtr = ct.POINTER(UI8Ptr)
ct_arr = np.ctypeslib.as_ctypes(arr)
UI8PtrArr = UI8Ptr * ct_arr._length_
ct_ptr = ct.cast(UI8PtrArr(*(ct.cast(row, UI8Ptr) for row in ct_arr)), UI8PtrPtr)
如何将 ct_ptr 转换回 numpy 二维数组?
注:这是对
一种方式是经历以下状态:
- CTypes指针
- Python 列出
- NumPy 数组
2 个观察结果自动出现:
- 效率很低,但不幸的是我不知道如何摆脱 #2.(这是罪魁祸首)
- 指针包含有关基类型的信息,但不包含创建它的数组的维数,因此您必须 "save" 它们,因为当 "deconstructing"指针
code00.py:
#!/usr/bin/env python3
import sys
import ctypes as ct
import numpy as np
def main():
dim0 = 3
dim1 = 4
arr0 = np.empty([dim0, dim1], dtype=np.uint8)
print("Initial array:\n{0:}".format(arr0))
UI8Ptr = ct.POINTER(ct.c_uint8)
UI8PtrPtr = ct.POINTER(UI8Ptr)
ct_arr = np.ctypeslib.as_ctypes(arr0)
UI8PtrArr = UI8Ptr * ct_arr._length_
ct_ptr = ct.cast(UI8PtrArr(*(ct.cast(row, UI8Ptr) for row in ct_arr)), UI8PtrPtr)
arr1 = np.array([[ct_ptr[i][j] for j in range(dim1)] for i in range(dim0)], dtype=ct_ptr._type_._type_._type_)
print("\nFinal array:\n{0:}".format(arr1))
print("\nEqual arrays: {0:}".format(np.array_equal(arr0, arr1)))
if __name__ == "__main__":
print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
main()
print("\nDone.")
输出:
[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q058781199]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code00.py Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32 Initial array: [[ 96 100 101 115] [105 114 101 100] [ 96 96 32 115]] Final array: [[ 96 100 101 115] [105 114 101 100] [ 96 96 32 115]] Equal arrays: True Done.
有关此主题的更多信息,请查看