I was looking for a simple way to load DDS files in python.
I found this: http://pyffi.sourceforge.net/ (but I am using pythin 2.7 so I can’t use it, it requires python3) Has anyone used that, or have another way of easily dealing with DDS file loading/viewing?
I also noticed, that pyglet appears might have some ability to load DDS (I didn’t try this): https://github.com/agileperception/pyglet/blob/master/tools/ddsview.py
I found this reference here: Loading a DDS to a QImage using QGLPixelBuffer. · GitHub
For anyone else looking … I converted that to python (made it a simple class) that returns a QImage - Most tools, I am often displaying stuff in Pyside/PyQT anyway.
#!/usr/bin/env python
#coding:utf-8
#reference: https://gist.github.com/bjorn/4635382
__author__ = 'jgalloway@bluepointgames.com'
# -- This line is 75 characters -------------------------------------------
# example code
#QImage readDDSFile(const QString &filename)
#{
#QGLWidget glWidget;
#glWidget.makeCurrent();
#GLuint texture = glWidget.bindTexture(filename);
#if (!texture)
#return QImage();
#// Determine the size of the DDS image
#GLint width, height;
#glBindTexture(GL_TEXTURE_2D, texture);
#glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
#glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
#if (width == 0 || height == 0)
#return QImage();
#QGLPixelBuffer pbuffer(QSize(width, height), glWidget.format(), &glWidget);
#if (!pbuffer.makeCurrent())
#return QImage();
#pbuffer.drawTexture(QRectF(-1, -1, 2, 2), texture);
#return pbuffer.toImage();
#}
#from PySide import QtGui, QtCore
import os, sys
from OpenGL.GL import *
#import numpy as np
from PySide.QtOpenGL import *
from PySide.QtGui import *
from PySide.QtCore import *
# global debug config set up in my tool setup
global gDebug
from bp_config.debugConfig import gDebug, setDebug
# my path convenience class, should be easy to remove/replace
from bp_files.bp_path import Path
###########################################################################
## DDS file reader
class fileDDS(object):
"""This class represnts a generic DDS file as a QImage"""
global gDebug
# --BASE-METHODS-------------------------------------------------------
# --constructor-
def __init__(self, filePath=None):
self._filePath = None
self._width = int(0)
self._height = int(0)
self._qImage = None
if filePath != None:
self._filePath = Path(filePath)
self._qImage = self.readDDSFile(self._filePath)
# --method-------------------------------------------------------------
def readDDSFile(self, filePath=None):
glWidget = QGLWidget()
glWidget.makeCurrent()
texture = glWidget.bindTexture(filePath)
if not texture:
return 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 gDebug:
print 'Image W/H:: {0}, {1}'.format(self._width, self._height)
if self._width == 0 and self._height == 0:
return QImage()
pbuffer = QGLPixelBuffer(QSize(self._width, self._height), glWidget.format())
if not pbuffer.makeCurrent():
return QImage()
pbuffer.drawTexture(QRectF(-1, -1, 2, 2), texture)
return pbuffer.toImage()
###########################################################################
# --call block-------------------------------------------------------------
if __name__ == "__main__":
'''Allows the script to be run as main()'''
global gDebug
gDebug = setDebug(True)
global myApp
myApp = QApplication(sys.argv)
myApp.setStyle('Plastique')
testPath = Path(r'testDDS.dds')
image = fileDDS(testPath)._qImage
# Should get back a QImage
print image
sys.exit(myApp.exec_())