使用 pyopengl glTexImage2D 时出现 OSError

OSError when using pyopengl glTexImage2D

Traceback (most recent call last):
  File "./x.py", line 132, in <module>
    texid = glutils.loadTexture("wall.png")
  File "C:\Userse25h\Desktop\Mess\py-opengl-error\glutils.py", line 31, in loadTexture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.size[0], img.size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, imgData)
  File "src/latebind.pyx", line 39, in OpenGL_accelerate.latebind.LateBind.__call__
  File "src/wrapper.pyx", line 311, in OpenGL_accelerate.wrapper.Wrapper.__call__
  File "C:\Userse25h\AppData\Local\Programs\Python\Python37\lib\site-packages\OpenGL\platform\baseplatform.py", line 415, in __call__
    return self( *args, **named )
OSError: exception: access violation reading 0x0000020F6D752000

这个错误我无法解决... 我想我不能描述得更详细 我的代码:

def loadTexture(filename):
    """load OpenGL 2D texture from given image file"""
    img = Image.open(filename) 
    imgData = numpy.array(list(img.getdata()), np.int8)
    texture = glGenTextures(1)
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    glBindTexture(GL_TEXTURE_2D, texture)
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.size[0], img.size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, imgData)
    glBindTexture(GL_TEXTURE_2D, 0)
    return texture

我该如何解决这个问题?帮帮我,谢谢

图像似乎没有 4 个颜色通道。该图像很可能只有 3 个颜色通道。您必须在 glTexImage2D 中指定格式 GL_RGB 而不是 GL_RGBA。例如:

format = GL_RGB if imgData.shape[1] == 3 else GL_RGBA
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.size[0], img.size[1], 0, 
    format, GL_UNSIGNED_BYTE, imgData)