OpenImageIO

3.1.12.0 · active · verified Thu Apr 16

OpenImageIO (OIIO) is a high-performance, open-source library and toolset for reading, writing, and processing images in a wide variety of file formats, using a format-agnostic API. Primarily aimed at visual effects (VFX) and animation applications, it provides robust support for formats like OpenEXR, TIFF, JPEG, PNG, and many more. The library is actively maintained by the Academy Software Foundation, with frequent minor releases within its major versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an in-memory image (a red square) using OpenImageIO and NumPy, write it to an OpenEXR file, and then read it back. It utilizes `ImageSpec` to define image properties and `ImageBuf` for convenient image manipulation and I/O. The `get_pixels` method retrieves pixel data as a NumPy array.

import OpenImageIO as oiio
import numpy as np
import os

# Create a dummy image (e.g., a red square)
width, height, channels = 256, 256, 3
spec = oiio.ImageSpec(width, height, channels, oiio.TypeDesc('float'))
# Create a NumPy array with red pixels
pixels = np.zeros((height, width, channels), dtype=np.float32)
pixels[:, :, 0] = 1.0 # Red channel to full intensity

# Create an ImageBuf from the spec and pixels
img_buf = oiio.ImageBuf(spec, pixels)

# Define output file path
output_filename = 'red_square.exr'

# Write the image
try:
    img_buf.write(output_filename)
    print(f"Successfully wrote {output_filename}")

    # Read the image back
    read_img_buf = oiio.ImageBuf(output_filename)
    if read_img_buf.has_error:
        raise RuntimeError(f"Error reading {output_filename}: {read_img_buf.geterror()}")
    
    # Get pixel data as a NumPy array
    read_pixels = read_img_buf.get_pixels(oiio.TypeDesc('float'))
    print(f"Read image with shape: {read_pixels.shape}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    if os.path.exists(output_filename):
        os.remove(output_filename) # Clean up the dummy file

view raw JSON →