Python Stubs for PyOpenColorIO

2.4.2.0 · active · verified Thu Apr 16

types-opencolorio provides static type checking stubs for PyOpenColorIO, the Python bindings for OpenColorIO (OCIO). OCIO is a complete color management solution for motion picture production, emphasizing visual effects and computer animation. This stub package enhances IDE support and enables tools like MyPy for type validation of PyOpenColorIO code. It closely follows the versioning of the main OpenColorIO library, which typically has annual releases around September.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load an OpenColorIO configuration and perform a basic color space transformation using the Python bindings. It first attempts to load an existing configuration (e.g., via the `OCIO` environment variable) and falls back to creating a minimal in-memory configuration if none is found. It then converts a sample sRGB pixel value to a linear color space using a `Processor`.

import PyOpenColorIO as OCIO
import os

# For a runnable example, we'll try to get the current config.
# In a real setup, you'd likely set the OCIO environment variable
# to point to an OCIO config file, e.g., os.environ['OCIO'] = '/path/to/my_config.ocio'
# For this example, we'll assume a default or simple config if OCIO is not set.

try:
    config = OCIO.GetCurrentConfig()
    print("Successfully loaded OCIO config.")
except OCIO.Exception as e:
    print(f"Could not load OCIO config: {e}. Attempting to create a minimal default config.")
    # Create a minimal config for demonstration if none is found
    config = OCIO.Config.Create()
    config.setFileFormatVersion(OCIO.Constants.OCIO_FILE_FORMAT_VERSION_2_0)
    linear_cs = OCIO.ColorSpace.Create()
    linear_cs.setName("linear")
    linear_cs.setFamily("linear")
    linear_cs.setIsData(False)
    config.addColorSpace(linear_cs)
    
    srgb_cs = OCIO.ColorSpace.Create()
    srgb_cs.setName("sRGB")
    srgb_cs.setFamily("display")
    srgb_cs.setIsData(False)
    srgb_to_linear = OCIO.BuiltinTransform.Create(OCIO.BuiltinTransformRegistry.ACES_CG_TO_ACES2065_1)
    srgb_to_linear.setDirection(OCIO.TransformDirection.INVERSE_TRANSFORM)
    srgb_cs.setTransform(srgb_to_linear, OCIO.TransformDirection.FROM_REFERENCE)
    config.addColorSpace(srgb_cs)
    
    config.setDefaultWorkingSpaceName("linear")
    config.setRole(OCIO.ROLE_SCENE_LINEAR, "linear")
    config.setRole(OCIO.ROLE_COLOR_PICKING, "sRGB")
    config.setRole(OCIO.ROLE_DISPLAY, "sRGB")


# Define source and destination color spaces
source_colorspace = "sRGB"
destination_colorspace = "linear"

# Ensure the color spaces exist in the config
if config.getColorSpace(source_colorspace) is None:
    print(f"Error: Source colorspace '{source_colorspace}' not found in config.")
    exit(1)
if config.getColorSpace(destination_colorspace) is None:
    print(f"Error: Destination colorspace '{destination_colorspace}' not found in config.")
    exit(1)

# Get a Processor to perform the color transformation
processor = config.getProcessor(OCIO.ColorSpaceTransform(src=source_colorspace, dst=destination_colorspace))

# Create an image buffer (e.g., a single pixel, RGB float values 0-1)
# Example sRGB value (mid-gray)
input_pixel = [0.218, 0.218, 0.218, 1.0] # R, G, B, A

# Create a working buffer (CPU processor operates on this)
# OCIO expects a contiguous flat array of pixel data.
# For a single pixel, it's [R, G, B, A]
output_pixel_data = list(input_pixel)

# Apply the transformation
# CPUProcessor handles float arrays directly
cpu_processor = processor.getCPUProcessor()
cpu_processor.applyRGB(output_pixel_data)

print(f"Input (sRGB): {input_pixel}")
print(f"Output (linear): {output_pixel_data[:3]} (alpha: {output_pixel_data[3]})")

view raw JSON →