如何将 Raw .NEF 文件加载到 QImage 中而不将其保存在 Python
How to load a Raw .NEF file into QImage without saving it in Python
开始做事的。我是 Python 图像工作流程的新手。但我总体上相当擅长 python。
但我似乎无法找到一个好的解决方案来显示 python 中尼康相机的 RAW .NEF 文件。
我想要做的是打开 RAW 文件,将其转换为 QImage 或 QPixmap,然后在不保存图像的情况下显示它。我尝试过 PIL、rawpy 和 numpy 等库。但我听说原始文件中通常有一个嵌入的 JPEG。有没有一种巧妙的方法来提取它?
根据我在互联网上发现的内容,人们转换原始文件,然后将其另存为 jpeg。但是我需要显示它而不保存它。
with rawpy.imread(file) as raw:
rgb = raw.postprocess()
rgb = np.transpose(rgb, (1, 0, 2)).copy()
h, w, a = rgb.shape
qim = QtGui.QImage(rgb, w, h, QtGui.QImage.Format_RGB888)
这就是我现在一直在尝试的。但是那个 returns None.
fd = file(file_path, 'rb')
# Skip over header + directory
# See manufacture specification
offset = 16 # Magic bytes
offset += 12 # Version
offset += 32 # Camera name
offset += 24 # Directory start & meta
fd.seek(offset, 0)
jpeg_offset = unpack('i', fd.read(4)) # Read where JPEG data starts
jpeg_length = unpack('i', fd.read(4)) # Read size of JPEG data
fd.seek(jpeg_offset[0], 0)
jpg_blob = fd.read(jpeg_length[0])
im = QtGui.QImage(jpg_blob)
这就是我试图提取嵌入的 JPEG。但那也 return None.
提前致谢。
使用将numpy图像转换为QImage。
import os
import sys
import rawpy
from PySide import QtCore, QtGui
def read_nef(path):
image = QtGui.QImage()
with rawpy.imread(path) as raw:
src = raw.postprocess()
h, w, ch = src.shape
bytesPerLine = ch * w
buf = src.data.tobytes() # or bytes(src.data)
image = QtGui.QImage(buf, w, h, bytesPerLine, QtGui.QImage.Format_RGB888)
return image.copy()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = QtGui.QLabel(scaledContents=True)
w.resize(640, 480)
w.show()
current_dir = os.path.dirname(os.path.realpath(__file__))
# http://www.luminescentphoto.com/nx2/nefs.html
filepath = os.path.join(current_dir, "baby.nef")
image = read_nef(filepath)
w.setPixmap(QtGui.QPixmap.fromImage(image))
sys.exit(app.exec_())
开始做事的。我是 Python 图像工作流程的新手。但我总体上相当擅长 python。 但我似乎无法找到一个好的解决方案来显示 python 中尼康相机的 RAW .NEF 文件。 我想要做的是打开 RAW 文件,将其转换为 QImage 或 QPixmap,然后在不保存图像的情况下显示它。我尝试过 PIL、rawpy 和 numpy 等库。但我听说原始文件中通常有一个嵌入的 JPEG。有没有一种巧妙的方法来提取它? 根据我在互联网上发现的内容,人们转换原始文件,然后将其另存为 jpeg。但是我需要显示它而不保存它。
with rawpy.imread(file) as raw:
rgb = raw.postprocess()
rgb = np.transpose(rgb, (1, 0, 2)).copy()
h, w, a = rgb.shape
qim = QtGui.QImage(rgb, w, h, QtGui.QImage.Format_RGB888)
这就是我现在一直在尝试的。但是那个 returns None.
fd = file(file_path, 'rb')
# Skip over header + directory
# See manufacture specification
offset = 16 # Magic bytes
offset += 12 # Version
offset += 32 # Camera name
offset += 24 # Directory start & meta
fd.seek(offset, 0)
jpeg_offset = unpack('i', fd.read(4)) # Read where JPEG data starts
jpeg_length = unpack('i', fd.read(4)) # Read size of JPEG data
fd.seek(jpeg_offset[0], 0)
jpg_blob = fd.read(jpeg_length[0])
im = QtGui.QImage(jpg_blob)
这就是我试图提取嵌入的 JPEG。但那也 return None.
提前致谢。
使用
import os
import sys
import rawpy
from PySide import QtCore, QtGui
def read_nef(path):
image = QtGui.QImage()
with rawpy.imread(path) as raw:
src = raw.postprocess()
h, w, ch = src.shape
bytesPerLine = ch * w
buf = src.data.tobytes() # or bytes(src.data)
image = QtGui.QImage(buf, w, h, bytesPerLine, QtGui.QImage.Format_RGB888)
return image.copy()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = QtGui.QLabel(scaledContents=True)
w.resize(640, 480)
w.show()
current_dir = os.path.dirname(os.path.realpath(__file__))
# http://www.luminescentphoto.com/nx2/nefs.html
filepath = os.path.join(current_dir, "baby.nef")
image = read_nef(filepath)
w.setPixmap(QtGui.QPixmap.fromImage(image))
sys.exit(app.exec_())