尝试在PYQT5中查看EXR文件时Python崩溃

时间:2018-10-18 17:33:35

标签: python pyqt5 openexr

我正在尝试加载两个exr文件,并将它们加载到gui的标签中,以便可以并排查看这两个文件。我可以让一个工作,但是当我尝试加载两个python崩溃时。下面是我的代码:

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)

如果您需要其他详细信息,请告诉我。预先感谢。

这是图像文件 http://www.mediafire.com/file/emm0vhhuwpwdx6v/exr_files.zip/file

1 个答案:

答案 0 :(得分:0)

由于您未提供MCVE,因此无法重现该问题,因此我将无法指出错误,因此,我将向您显示有效代码,在这种情况下,我将假定图像已打开.py的侧面:

├── chip.exr
├── main.py
└── panel.exr

代码:

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

enter image description here