def process_layer(r): if not isinstance(r, np.ndarray): r = np.asarray(r).astype(float) / 255.0 if r.ndim < 3: r2 = np.empty((r.shape[0], r.shape[1], 3), dtype=float) r2[:, :, :] = r[:, :, np.newaxis] r = r2 r /= np.percentile(r, 99.9) return rgb_to_srgb(r)
def open_multilayer_exr(filename, tonemap=False, thumb_size=0, show_progress=False): """ Load a multilayer OpenEXR file and return a dictionary mapping layers to either numpy float32 arrays (if ``tonemap=False``) or to 8bit PIL images (if ``tonemap=True``). :param filename: string filename :param tonemap: if ``True``, map to sRGB :param thumb_size: if nonzero, resize images to have this as their max dimension :param show_progress: if ``True``, print info about loading """ if show_progress: print "Reading %s: %s layers..." % (filename, len(LAYER_CHANNELS)) # Open the input file f = OpenEXR.InputFile(filename) header = f.header() # Compute the size dw = header['dataWindow'] cols, rows = dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1 multilayer = {} # load channels FLOAT = Imath.PixelType(Imath.PixelType.FLOAT) for key, channels in LAYER_CHANNELS.iteritems(): print "Loading layer %s..." % key image = np.empty((rows, cols, 3), dtype=np.float32) for (i, c) in enumerate(channels): data = f.channel(c, FLOAT) image[:, :, i] = np.fromstring(data, dtype=np.float32) \ .reshape((rows, cols)) multilayer[key] = image #if denoise: #for t in ["diff", "gloss", "trans"]: #print "Denoising layer %s..." % t #multilayer["%s_ind" % t] = denoise_indirect_image(multilayer["%s_ind" % t]) ## recompute combined image using denoised layer #multilayer["combined"] = multilayer["emit"] + multilayer["env"] #for t in ["diff", "gloss", "trans"]: #multilayer["combined"] += ( #multilayer["%s_col" % t] * (multilayer["%s_dir" % t] + multilayer["%s_ind" % t]) #) # resize and tonemap if tonemap: for key, channels in LAYER_CHANNELS.iteritems(): print "Tonemapping layer %s..." % key image = multilayer[key] if key == "depth": image /= np.max(image[np.isfinite(image)]) # convert to sRGB PIL image = numpy_to_pil(rgb_to_srgb(np.clip(image, 0.0, 1.0))) if thumb_size and key != "combined": image = ResizeToFit(thumb_size, thumb_size).process(image) multilayer[key] = image return multilayer