我已经为此挣扎了一段时间了,我觉得我已经用尽了所有的选择,所以我希望有人能帮助我;
我正在尝试加载一堆DDS文件,将它们转换为QImage对象并在QGraphicsView中显示它们。到目前为止,我一直在进步,但现在我似乎遇到了一堵无法逾越的墙。到目前为止,我已经参考了这些参考资料,但没有解决我的问题:
tech-artists帖子
github pos
pyqt4参考
QT论坛帖子,这是它真正开始的地方
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 Groups讨论基本上做同样的事情(似乎都是从最后一个链接开始的)
我目前在PyQt5, Python版本3.5.0
问题是:在PyaQt5中,QGLPixelBuffer似乎不受支持(无论我试图从哪个模块导入它,我似乎都无法导入它,所以我认为它是),所以我仅限于使用QGLWidget作为我的渲染平台。然而,上面的代码似乎没有正确地调整我的纹理大小。不管我怎么做,它们总是被渲染得太小了。
我从techhartists线程(无论如何)中获取了这段代码,不幸的是,对于drawTexture rect被设置为(-1,-1,2,2)的原因,从来没有给出解释,所以虽然我真的不明白那里发生了什么,我认为这可能是问题所在。
如果有人对此有任何想法,我将非常感激……
欢呼
我想我会把我找到的答案贴出来,因为我花了一天半的时间搜索这个,知道这个很有用;
我最终使用pyglet解决了这个问题,pyglet对大多数图像文件格式都有原生支持,并且可以很容易地返回一个像素数据数组,QImage类可以读取这些数据来创建一个QImage类。代码看起来有点像这样:
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