'glReadPixels How to get pixel value
I use this function to return such a value. I hope to get the RGB value of each pixel, which is 0 to 255, not 0 to 1. What should I do
data = glReadPixels(0,0, w, h, GL_RGB,GL_UNSIGNED_BYTE)
print(data)
output
\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad\xd1\x9e\xad
Solution 1:[1]
Use numpy.frombuffer to convert the string to a byte array:
import numpy
data = glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE)
byte_array = numpy.frombuffer(data, dtype='uint8')
print(list(byte_array))
Or use ctypes.from_buffer_copy
import ctypes
data = glReadPixels(0,0, w, h, GL_RGB,GL_UNSIGNED_BYTE)
byte_array = (ctypes.c_ubyte * (w * h * 3)).from_buffer_copy(data)
print(list(byte_array))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

