Multilayer to Multipart Python #4133
-
Hey, currently I am looking into converting multilayer exrs into multipart exrs with the python bindings.
import OpenImageIO as oiio
from OpenImageIO import ImageInput, ImageOutput
from OpenImageIO import ImageBuf, ImageSpec, ImageBufAlgo
from collections import OrderedDict
out_path = "out.exr"
in_path= "in.exr"
def is_multipart(path):
inp = ImageInput.open(path)
if inp:
return inp.seek_subimage(1, 0)
return None
def extract_layers(path):
img = ImageInput.open(path)
channels = img.spec().channelnames
layers = OrderedDict()
for i in channels:
layername = i[:-2]
layername = layername.replace("ViewLayer.","") #Maybe this can be fixed in Blender?
if layername == "Combined":
layername = "rgba"
if layername not in layers:
layers[layername] = [i]
else:
layers[layername].append(i)
return layers
def convert_to_multipart(path, out_path, half=None):
"""
convert a given exr to a multipart exr
"""
if is_multipart(path):
return
buf_spec = OrderedDict()
layers = extract_layers(path)
img = ImageBuf(path)
for layer_name, channels in layers.items():
print(channels)
buf = ImageBufAlgo.channels(img, tuple(channels))
# find out how to set the layername, maybe via spec ?
# set the bit depth of buf ??
# clean up metadata per subimage remove cryptometadata from subimages that are not cryptos
buf_spec[buf] = img.spec()
out = ImageOutput.create(out_path)
out.open(out_path, list(buf_spec.values())[0])
# not sure if this should be the list of all specs ?
# out.open(out_path, list(buf_spec.values()))
count = 0
for buf,spec in buf_spec.items():
print(buf)
if count>0:
out.open(out_path, spec, "AppendSubimage")
count += 1
buf.write(out)
out.close()
convert_to_multipart(in_path, out_path) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
This is now working for me and it is surprisingly fast. 2 Questions: |
Beta Was this translation helpful? Give feedback.
-
You seem to have already figured out that for openexr, you need to give the list ImageSpec's for all the parts up front. Some formats (like TIFF) let you add part by part incrementally, but others (like OpenEXR) simply must know about all the parts up front. It's fine to give the list up front even for the ones that don't need it, it will just remember them for later. As for your questions:
|
Beta Was this translation helpful? Give feedback.
This is now working for me and it is surprisingly fast.
https://gist.github.com/nebukadhezer/136608889ccd8357288997050ca7c67b
2 Questions:
Does the buf spec set the write method like tiled or scanline ?
so could an exr with subimages have one layer as tiled and another as scanline ?