MaVCArt
MaVCArt

Reputation: 803

PyQt5 Python 3.5.0: convert DDS File to QImage

I've been struggling with this for a while now and I feel like I have exhausted all my options so I hope someone can help me with this;

I'm trying to load a bunch of DDS files, convert them into QImage objects and display them in a QGraphicsView. So far, I've been making progress but now I've hit a wall I can't seem to pass. I've consulted these resources so far, with no luck in solving my problem:

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())

and a bunch of old Google Groups discussions basically doing the same thing ( it all seemed to start with that last link )

I'm currently on PyQt5, Python version 3.5.0

The problem is this: in PyaQt5, QGLPixelBuffer does not seem to be supported ( I can not seem to import it anyway, regardless of the module I try to import it from, so I presume it is ) so I'm limited to using the QGLWidget as my rendering platform. However, the code above does not seem to be sizing my textures correctly. They're consistently being rendered way too small, no matter what I do.

I took this code from the techartists thread ( most of it anyway ), and unfortunately an explanation is never given for the reason the drawTexture rect is set to (-1,-1,2,2), so while I don't really understand what's going on there, I presume that's likely to be where the problem is.

If anyone has any ideas about this, I would greatly greatly appreciate it...

Cheers

Upvotes: 2

Views: 650

Answers (1)

MaVCArt
MaVCArt

Reputation: 803

I thought I would post the answer I found seeing as I spent a day and a half searching for this, and it's useful to know about;

I ended up solving this using pyglet, which has native support for most image file formats, and can quite easily return an array of pixel data, which QImage classes can then read to create a QImage class. the code looks a bit like this:

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

Upvotes: 0

Related Questions