Python 尝试在 PYQT5 中查看 EXR 文件时崩溃
Python crashes trying to view EXR files in PYQT5
我正在尝试加载两个 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
由于您没有提供 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_())
我正在尝试加载两个 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
由于您没有提供 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_())