Skip to content

Instantly share code, notes, and snippets.

@ohtaman
Created May 20, 2017 14:58
Show Gist options
  • Select an option

  • Save ohtaman/a47cc5f3c20356ce7d37dd48bd77efc9 to your computer and use it in GitHub Desktop.

Select an option

Save ohtaman/a47cc5f3c20356ce7d37dd48bd77efc9 to your computer and use it in GitHub Desktop.
メモリのアドレスからnp.arrayを生成
from ctypes import c_char_p, c_byte, cast, ARRAY, POINTER
import numpy as np
a = np.array(list(range(10)), dtype=np.uint8)
a_addr = a.__array_interface__['data'][0]
size = len(a)
a_char_p = c_char_p(a_addr)
a_byte_array = cast(a_char_p, POINTER(ARRAY(c_byte, len=size))).contents
b = np.frombuffer(a_byte_array, dtype=np.uint8)
print(a)
print(b)
a[1] = 3
print(a)
print(b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment