2016-06-28 35 views
1

我一直在爲此掙扎了一段時間,我覺得我已經用盡了所有選項,所以我希望有人能夠幫助我解決這個問題。PyQt5 Python 3.5.0:將DDS文件轉換爲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()) 

和一幫老谷歌的:我已經在解決我的問題,到目前爲止,諮詢這些資源,沒有運氣小組討論基本上是做同樣的事情(這一切似乎開始於此最後一個環節)

目前我在PyQt5,Python版本3.5.0

問題是這樣的:在 PyaQt5,QGLPixelBuffer似乎不支持(我似乎無法反正導入它,無論我嘗試導入它的模塊,所以我認爲它是),所以我僅限於使用QGLWidget作爲我的渲染平臺。但是,上面的代碼似乎沒有正確調整我的紋理大小。無論我做什麼,他們一直被渲染得太小。

我從techartists線程(大部分都是這個)得到了這段代碼,不幸的是,由於drawTexture矩形被設置爲(-1,-1,2,2),所以不會給出解釋,所以當我真的不明白那裏發生了什麼,我認爲這可能是問題所在。

如果任何人有這個任何想法,我將不勝不勝感激......

乾杯

回答

0

我想我會後我發現看到,因爲我花了一天的答案,半搜索這一點,這是有用的知道;

我最終解決了這個問題,使用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