PyQt5 Python 3.5.0: 将 DDS 文件转换为 QImage

PyQt5 Python 3.5.0: convert DDS File to QImage

我已经为此苦苦挣扎了一段时间,我觉得我已经用尽了所有的选择,所以我希望有人能帮助我解决这个问题;

我正在尝试加载一堆 DDS 文件,将它们转换为 QImage 对象并将它们显示在 QGraphicsView 中。到目前为止,我一直在取得进步,但现在我遇到了我似乎无法通过的墙。到目前为止,我已经查阅了这些资源,但没有成功解决我的问题:

tech-artists post

github pos

pyqt4 reference

QT forum post, this is where it really started

def readDDSFile(self, filePath, width, height):
    glWidget = QGLWidget()
    glWidget.makeCurrent()
    glWidget.setGeometry(0,0,width,height) # init width and height, in an attempt to force the widget to be the same size as the texture

    # works fine, DDS file loads without problem
    texture = glWidget.bindTexture(filePath)
    if not texture:
        return QtGui.QImage()

    # Determine the size of the DDS image
    glBindTexture(GL_TEXTURE_2D, texture)

    self._width =  glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH)
    self._height = glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT)

    if self._width == 0 and self._height == 0:
        return QtGui.QImage()

    # up to here, everything works fine, DDS files are being loaded, and, in the line below, rendered. They're just being rendered way too small.
    glWidget.drawTexture(QtCore.QRectF(-1,-1,2,2), texture)
    return (glWidget.grabFrameBuffer())

和一堆旧的 Google 小组讨论基本上在做同样的事情(这一切似乎都是从最后一个 link 开始的)

我目前使用的是 PyQt5,Python 版本 3.5.0

问题是这样的: 在 PyaQt5 中,QGLPixelBuffer 似乎不受支持(我似乎无法导入它,无论我尝试从哪个模块导入它,所以我认为它是)所以我仅限于使用 QGLWidget 作为我的渲染平台.但是,上面的代码似乎没有正确调整我的纹理大小。无论我做什么,它们总是被渲染得太小。

我从技术艺术家线程中获取了这段代码(大部分都是),不幸的是,由于 drawTexture rect 设置为 (-1,-1,2,2) 的原因,从未给出解释,所以虽然我真的不明白那里发生了什么,我想这可能就是问题所在。

如果有人对此有任何想法,我将不胜感激...

干杯

我想我会 post 我花了一天半的时间搜索这个找到的答案,了解它很有用;

我最终使用 pyglet 解决了这个问题,它原生支持大多数图像文件格式,并且可以很容易地 return 像素数据数组,QImage classes然后可以读取创建一个QImage class。代码看起来有点像这样:

import pyglet

def readDDSFile(filePath):
    _img = pyglet.image.load(filePath)
    _format = tex.format
    pitch = tex.width * len(_format)
    pixels = tex.get_data(_format, pitch)

    img = QtGui.QImage(pixels, tex.width, tex.height, QtGui.QImage.Format_RGB32)
    img = img.rgbSwapped()

    return img