0

I'm attempting to write a floating point PIL image object out to a channel in an EXR file using OpenEXR.

I can read EXR data into a PIL image fine:

import OpenEXR
import Imath
from PIL import Image
import numpy as np

exrPath = "path/to/image.exr"
exrFile = OpenEXR.InputFile(exrPath)

pt = Imath.PixelType(Imath.PixelType.FLOAT)
dw = curFile.header()['dataWindow']
size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)

rgbf = [Image.frombytes("F", size, exrFile.channel(c, pt)) for c in ("R", "G", "B")]

I then run some operations over the PIL image data and want to save a single channel out as a new EXR. This is what I have so far:

exrHeader = OpenEXR.Header(pilImage.size[0],pilImage.size[1])
exrHeader["channels"] = {"GRAY":Imath.Channel(Imath.PixelType(Imath.PixelType.FLOAT), 1, 1)}

exrOut = OpenEXR.OutputFile("path/to/new.exr", exrHeader)
exrOut.writePixels({"GRAY": np.array(pilImage).astype(np.float32).tostring()})

But I get this error:

TypeError: Data for channel 'GRAY' should have size 67108864 but got 16777216

How do I convert a floating point PIL image to the correct format to write to a float EXR channel?

4

1 回答 1

1

I got it working but don't understand it completely yet.

npImage = np.squeeze(pilImage)
size = img.shape
exrHeader = OpenEXR.Header(size[1], size[0])

exrHeader['channels'] = {"GRAY":Imath.Channel(Imath.PixelType(Imath.PixelType.FLOAT), 1, 1)}

exrOut = OpenEXR.OutputFile("path/to/new.exr", exrHeader)
GRAY = (npImage[:,:]).astype(np.float32).tobytes()
exrOut.writePixels({'GRAY' : R})
exrOut.close()
于 2021-01-07T17:26:05.560 回答