Skip to content

Instantly share code, notes, and snippets.

@devilholk
Created September 23, 2016 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilholk/5a7bd8a3e5b0b8204886ff48115b7c39 to your computer and use it in GitHub Desktop.
Save devilholk/5a7bd8a3e5b0b8204886ff48115b7c39 to your computer and use it in GitHub Desktop.
Some notes about using ctypes for accessing structured data
import ctypes
class rgba(ctypes.Structure):
_fields_ = (
('r', ctypes.c_uint8),
('g', ctypes.c_uint8),
('b', ctypes.c_uint8),
('a', ctypes.c_uint8),
)
width = 3
height = 3
#Skapa 9 pixlar
data = (rgba * 9)(*(rgba(i, i+20, i+50, i+100) for i in range(9)))
data_ptr = ctypes.addressof(data)
print('Pixlarna finns på', hex(data_ptr))
pixels = (rgba*width*height).from_address(data_ptr)
x = 1
y = 2
p = pixels[y][x]
print(p.r, p.g, p.b, p.a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment