def np_to_imagebuf(cls, img_pixels: np.array): """ Load a numpy array 8/32bit to oiio ImageBuf """ if len(img_pixels.shape) < 3: LOGGER.error( 'Can not create image with pixel data in this shape. Expecting 4 channels(RGBA).' ) return h, w, c = img_pixels.shape img_spec = ImageSpec(w, h, c, cls.get_numpy_oiio_img_format(img_pixels)) img_buf = ImageBuf(img_spec) img_buf.set_pixels(img_spec.roi_full, img_pixels) return img_buf
def OIIOImageBufferFromOpenCVImageBuffer(opencvImageBuffer): (height, width, channels) = opencvImageBuffer.shape npChanneltype = opencvImageBuffer.dtype #print( "OIIOImageBufferFromOpenCVImageBuffer", width, height, channels, npChanneltype ) npToArrayBitDepth = { np.dtype('uint8') : 'B', np.dtype('uint16') : 'H', np.dtype('uint32') : 'I', np.dtype('float32') : 'f', np.dtype('float64') : 'd', } npToOIIOBitDepth = { np.dtype('uint8') : oiio.BASETYPE.UINT8, np.dtype('uint16') : oiio.BASETYPE.UINT16, np.dtype('uint32') : oiio.BASETYPE.UINT32, np.dtype('float32') : oiio.BASETYPE.FLOAT, np.dtype('float64') : oiio.BASETYPE.DOUBLE, } # Support this when oiio more directly integrates with numpy # np.dtype('float16') : oiio.BASETYPE.HALF, if (npChanneltype in npToArrayBitDepth and npChanneltype in npToOIIOBitDepth): arrayChannelType = npToArrayBitDepth[npChanneltype] oiioChanneltype = npToOIIOBitDepth[npChanneltype] else: print( "opencv to oiio - Using fallback bit depth" ) arrayChannelType = 'f' oiioChanneltype = oiio.BASETYPE.FLOAT spec = ImageSpec(width, height, channels, oiioChanneltype) oiioImageBuffer = ImageBuf(spec) roi = oiio.ROI(0, width, 0, height, 0, 1, 0, channels) conversion = oiioImageBuffer.set_pixels( roi, array.array(arrayChannelType, opencvImageBuffer.flatten()) ) if not conversion: print( "opencv to oiio - Error converting the OpenCV buffer to an OpenImageIO buffer" ) oiioImageBuffer = None return oiioImageBuffer