简体   繁体   English

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

[英]Python crashes trying to view EXR files in PYQT5

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. 我正在尝试加载两个exr文件,并将它们加载到gui的标签中,以便可以并排查看这两个文件。 I can get one to work but when I try to load both python crashes. 我可以让一个工作,但是当我尝试加载两个python崩溃时。 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 这是图像文件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: 由于您没有提供MCVE,因此无法重现该问题,因此,我将无法指出错误,因此,我将向您展示有效的代码,在这种情况下,我将假定图像在侧面。 .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_())

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM