954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Opening BMP 16bit 565 files created from GIMP

Hi,

I've got some trouble opening files saved by The Gimp in BMP 16bits 565. I've test some various bmp they all work except those saved from The Gimp and I really don't know why the format might be different from the one found there: http://wvnvaxa.wvnet.edu/vmswww/bmp.html
Bleu.bmp and Mire.bmp are some files that can't be show properly while test16bf565.bmp can be open.
I've written a little program that load BMP and show them by a label with a pyQt window.
It looks like this :

from PyQt4 import QtGui, QtCore
import sys, os

class Ui_ShowBmp(QtGui.QMainWindow):
    def setupUi(self, ShowBmp):
        ShowBmp.setObjectName("ShowBmp")
        ShowBmp.resize(660, 529)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(ShowBmp.sizePolicy().hasHeightForWidth())
        ShowBmp.setSizePolicy(sizePolicy)
        self.centralwidget = QtGui.QWidget(ShowBmp)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout_2 = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setPixmap(QtGui.QPixmap("Sample.bmp"))
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.loadButton = QtGui.QPushButton(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.loadButton.sizePolicy().hasHeightForWidth())
        self.loadButton.setSizePolicy(sizePolicy)
        self.loadButton.setObjectName("loadButton")
        self.verticalLayout.addWidget(self.loadButton)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        ShowBmp.setCentralWidget(self.centralwidget)

        self.retranslateUi(ShowBmp)
        QtCore.QMetaObject.connectSlotsByName(ShowBmp)

    def retranslateUi(self, ShowBmp):
        ShowBmp.setWindowTitle(QtGui.QApplication.translate("ShowBmp", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.loadButton.setText(QtGui.QApplication.translate("ShowBmp", "Load", None, QtGui.QApplication.UnicodeUTF8))

class Ui_Extension(Ui_ShowBmp):
    def __init__ (self):
        self.app = QtGui.QApplication(sys.argv)
        QtGui.QMainWindow.__init__(self)
        self.window = QtGui.QMainWindow()
        self.setupUi(self.window)
        self.createSignals()
        self.window.show()
        self.curdir = os.getcwd()
        (self.xsize,self.ysize) = ("0","0")
        
    def createSignals(self):
        QtCore.QObject.connect(self.loadButton, QtCore.SIGNAL("clicked()"), self.pushedLoadBmpButton)
    def pushedLoadBmpButton(self):
        self.imagepath = QtGui.QFileDialog.getOpenFileName(self,"Select BMP", self.curdir, ("BMP (*.bmp)"))
        (self.xsize,self.ysize) = self.getSize(self.imagepath)
        self.image = QtGui.QImage(str(self.imagepath))
        self.label.setGeometry(10, 10,int(self.xsize), int(self.ysize))
        self.label.setPixmap(QtGui.QPixmap.fromImage(self.image))
    def getSize(self,filepath): 
'''Return the size from the bmp loaded'''
        file = open(filepath,"rb")
        fileread = file.read()
        file.close()
        header = fileread[:71]
        xpixels = header[21:17:-1]
        xlist = []
        for c in xpixels:
            data = str(hex(ord(c)))
            if len(data) == 3:
                data = 'x0'.join(data.split('x'))
            xlist.append(data)
        xsize = int('0x'+''.join(''.join(xlist).split('0x')),base = 16)

        ypixels = header[25:21:-1]
        ylist = []
        for c in ypixels:
            data = str(hex(ord(c)))
            if len(data) == 3:
                data = 'x0'.join(data.split('x'))
            ylist.append(data)
        ysize = int('0x'+''.join(''.join(ylist).split('0x')),base = 16)
        return (str(xsize),str(ysize))





class Ui_ShowBmp(QtGui.QMainWindow):
    def setupUi(self, ShowBmp):
        ShowBmp.setObjectName("ShowBmp")
        ShowBmp.resize(660, 529)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(ShowBmp.sizePolicy().hasHeightForWidth())
        ShowBmp.setSizePolicy(sizePolicy)
        self.centralwidget = QtGui.QWidget(ShowBmp)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout_2 = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setPixmap(QtGui.QPixmap("Sample.bmp"))
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.loadButton = QtGui.QPushButton(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.loadButton.sizePolicy().hasHeightForWidth())
        self.loadButton.setSizePolicy(sizePolicy)
        self.loadButton.setObjectName("loadButton")
        self.verticalLayout.addWidget(self.loadButton)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        ShowBmp.setCentralWidget(self.centralwidget)

        self.retranslateUi(ShowBmp)
        QtCore.QMetaObject.connectSlotsByName(ShowBmp)

    def retranslateUi(self, ShowBmp):
        ShowBmp.setWindowTitle(QtGui.QApplication.translate("ShowBmp", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.loadButton.setText(QtGui.QApplication.translate("ShowBmp", "Load", None, QtGui.QApplication.UnicodeUTF8))



def ShowBmp():
    ui = Ui_Extension()
    sys.exit(ui.app.exec_())

if __name__ == '__main__':
    ShowBmp()


Thanks in advance for your help

Attachments Bleu.bmp (600.07KB) Mire.bmp (600.07KB) test16bf565.bmp (16.06KB)
sebcbien
Newbie Poster
10 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

With a simple hex editor you can see that the GIMP file headers are completely different.

Cheers and happy coding

Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
 

Well I've actually check this before posting and I've seen that the headers were quite different. Does this mean I'm gonna have to create a homemade reader and decoder? I wish I could find an other way...

sebcbien
Newbie Poster
10 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: