如何在Python中创建无符号字符数组?(对于使用PyOpenGL的glReadPixels)

2024-03-29 00:21:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经使用PyOpenGL在GLES2和EGL中编写了一些代码,我需要使用glReadPixels函数,除了最后一个参数必须是ctypes unsigned char缓冲区,我不知道如何创建它

下面是C代码:

unsigned char* buffer = malloc(width * height * 4);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

等效的Python代码是什么

我使用的是GLES2而不是GL,因此,buffer = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE)不起作用

当我尝试buffer = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE)时,我得到以下错误:

   buffer = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE)
  File "/home/fa/berryconda3/lib/python3.6/site-packages/OpenGL/platform/baseplatform.py", line 415, in __call__
    return self( *args, **named )
TypeError: this function takes at least 7 arguments (6 given)

Tags: 函数代码bufferbytewidthheightpyopenglgl
1条回答
网友
1楼 · 发布于 2024-03-29 00:21:43

创建具有适当大小的字节缓冲区:

buffer_size = width * height * 4
buffer = (GLbyte * buffer_size)()

将缓冲区传递到以下像素:

glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer)

这与使用ctypes.c_byte相同:

import ctypes
buffer_size = width * height * 4
buffer = (ctypes.c_byte * buffer_size)()

或创建具有适当大小的numpy缓冲区:

import numpy
buffer = numpy.empty(width * height * 4, "uint8")

传递缓冲区glReadPixels

glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer)

相关问题 更多 >