Python Stubs for PyOpenColorIO
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
-
PyOpenColorIO.Exception: Error building YAML: Colorspace associated to the role 'color_picking', does not exist.
cause The OpenColorIO configuration file is trying to assign a color space role (e.g., 'color_picking') to a color space that is not defined within the configuration.fixEdit your `.ocio` configuration file to ensure that all referenced color spaces for roles actually exist, or update the role assignment to a valid color space. -
ImportError: DLL load failed while importing PyOpenColorIO: The specified module could not be found.
cause On Windows, starting with Python 3.8, changes to DLL loading behavior can prevent `PyOpenColorIO` from finding its required native libraries.fixThis issue is complex and often related to how `opencolorio` was built or installed. Possible solutions include ensuring all `opencolorio` dependencies are in the system PATH, or in some cases, reverting to Python versions prior to 3.8 if a pre-built wheel for your specific Python 3.8+ environment isn't correctly configured. For development builds, consult OpenColorIO's build documentation for Windows-specific considerations. -
AttributeError: module 'PyOpenColorIO' has no attribute 'ColorSpace'
cause This usually indicates that the `opencolorio` runtime library is not installed or incorrectly installed, leading to the type stubs for `PyOpenColorIO` being found, but the actual C++-backed Python module is not loaded correctly, or the version mismatch.fixVerify that `pip install opencolorio` completed successfully and that the installed `opencolorio` version matches the expected API for the `types-opencolorio` version you are using. If running in a virtual environment, ensure it's activated.
Warnings
- breaking OpenColorIO v2 introduced significant API changes compared to v1, including a re-engineered GPU renderer, modified clamping behavior for transforms, and new requirements for interchange roles (`aces_interchange`, `cie_xyz_d65_interchange`) for inter-config conversions.
- gotcha The `types-opencolorio` package provides only type stubs. The actual runtime library, `PyOpenColorIO` (installed via `pip install opencolorio`), must be installed separately for your Python code to execute successfully. Without it, you will encounter `ModuleNotFoundError` at runtime.
- gotcha The `OCIO` environment variable is crucial for OpenColorIO applications as it typically points to the active configuration file. If this variable is not set or points to an invalid/incomplete configuration, `OCIO.GetCurrentConfig()` will fail, or applications may behave unexpectedly.
Install
-
pip install types-opencolorio -
pip install opencolorio
Imports
- OCIO
import PyOpenColorIO as OCIO
- Config
import PyOpenColorIO as OCIO config = OCIO.Config()
import PyOpenColorIO as OCIO config = OCIO.GetCurrentConfig()
- ColorSpace
import PyOpenColorIO as OCIO cs = OCIO.ColorSpace()
Quickstart
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]})")