I'm trying to load two exr files and load them into labels on the gui so I can view the two files side by side. I can get one to work but when I try to load both python crashes. Below is my code:
def exrToJpgGamma(exrfile):
file = OpenEXR.InputFile(exrfile)
pt = Imath.PixelType(Imath.PixelType.FLOAT)
dw = file.header()['dataWindow']
size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
RedStr = file.channel('R', pt)
GreenStr = file.channel('G', pt)
BlueStr = file.channel('B', pt)
Red = array.array('f', RedStr)
Green = array.array('f', GreenStr)
Blue = array.array('f', BlueStr)
def EncodeToSRGB(v):
if (v <= 0.0031308):
return (v * 12.92) * 255.0
else:
return (1.055*(v**(1.0/2.2))-0.055) * 255.0
for I in range(len(Red)):
Red[I] = EncodeToSRGB(Red[I])
for I in range(len(Green)):
Green[I] = EncodeToSRGB(Green[I])
for I in range(len(Blue)):
Blue[I] = EncodeToSRGB(Blue[I])
rgbf = [Image.frombytes("F", size, Red.tobytes())]
rgbf.append(Image.frombytes("F", size, Green.tobytes()))
rgbf.append(Image.frombytes("F", size, Blue.tobytes()))
rgb8 = [im.convert("L") for im in rgbf]
myqimage = Image.merge("RGB", rgb8)
return myqimage
def showEXR(self):
width = 480
height = 360
imageq = PilImageQt(exrToJpgGamma(chip.exr))
qimage = QtGui.QImage(imageq)
pixmap = QtGui.QPixmap.fromImage(qimage)
ScaledPixmap = pixmap.scaled(width, height, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
self.chip_img.setPixmap(ScaledPixmap)
imageq = PilImageQt(exrToJpgGamma(panel.exr))
qimage = QtGui.QImage(imageq)
pixmap = QtGui.QPixmap.fromImage(qimage)
ScaledPixmap = pixmap.scaled(width, height, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
self.panel_img.setPixmap(ScaledPixmap)
return
showEXR(self)
Let me know if you need any additional details. Thanks in advance.
Here are the image files http://www.mediafire.com/file/emm0vhhuwpwdx6v/exr_files.zip/file
Since I can not reproduce the problem since you do not provide an MCVE, then I will not be able to point out the error, so I will show you a working code, in this case I will assume that the images are on the side of the .py:
├── chip.exr
├── main.py
└── panel.exr
Code:
import OpenEXR, Imath
from PIL import Image
import array
from PIL.ImageQt import ImageQt as PilImageQt
from PyQt5 import QtCore, QtGui, QtWidgets
def exrToJpgGamma(exrfile):
file = OpenEXR.InputFile(exrfile)
pt = Imath.PixelType(Imath.PixelType.FLOAT)
dw = file.header()['dataWindow']
size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
RedStr = file.channel('R', pt)
GreenStr = file.channel('G', pt)
BlueStr = file.channel('B', pt)
Red = array.array('f', RedStr)
Green = array.array('f', GreenStr)
Blue = array.array('f', BlueStr)
def EncodeToSRGB(v):
if v <= 0.0031308:
return (v * 12.92) * 255.0
else:
return (1.055*(v**(1.0/2.2))-0.055) * 255.0
for I, value in enumerate(Red):
Red[I] = EncodeToSRGB(value)
for I, value in enumerate(Green):
Green[I] = EncodeToSRGB(value)
for I, value in enumerate(Blue):
Blue[I] = EncodeToSRGB(value)
rgbf = [Image.frombytes("F", size, channel.tobytes()) for channel in (Red, Green, Blue)]
rgb8 = [im.convert("L") for im in rgbf]
myqimage = Image.merge("RGB", rgb8)
return myqimage
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.chip_img = QtWidgets.QLabel()
self.panel_img = QtWidgets.QLabel()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.chip_img)
lay.addWidget(self.panel_img)
self.load_exr('panel.exr', self.panel_img)
self.load_exr('chip.exr', self.chip_img)
def load_exr(self, filename, label):
width, height = 480, 360
imageq = PilImageQt(exrToJpgGamma(filename))
pixmap = QtGui.QPixmap.fromImage(imageq.copy())
scaled_pixmap = pixmap.scaled(width, height,
QtCore.Qt.KeepAspectRatio,
QtCore.Qt.FastTransformation)
label.setPixmap(scaled_pixmap)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())