OCIO - Python

I am writing a tool for Houdini that can create, save and apply color themes to color ramp parameters. In this project I needed to convert between color spaces. There are many ways to do this, but this led me to start exploring OCIO using Python. Below is a snippet that changes the color space of a list of rgb values from srgb to linear srgb.

  • First I activate the venv into which I want to install OCIO
  • Then I run pip install opencolorio
  • With the package installed make sure that your IDE can access the package
  • We also need to download a OCIO configuration file from here
import os
import PyOpenColorIO as OCIO

def apply_rgb(rgb, src_space, dst_space):

      config = OCIO.GetCurrentConfig()
      processor = config.getProcessor(src_space, dst_space);
      cpu = processor.getDefaultCPUProcessor()
      return cpu.applyRGB(rgb)

   except Exception as e:
      print("OpenColorIO Error: ", e)

os.environ["OCIO"] = 'C:/Users/Johan/Desktop/OpenColorIO-Configs-master/aces_1.0.3/config.ocio'

def print_color_spaces():
	config = OCIO.GetCurrentConfig()
	colorSpaceNames = [ cs.getName() for cs in config.getColorSpaces() ]
	print('\n'.join(c for c in colorSpaceNames))

val = apply_rgb(	rgb=[.75, .5, .25],
					src_space='Input - Generic - sRGB - Texture',
					dst_space='Utility - Linear - sRGB')

# print_color_spaces()