website statistics

OpenEXR bindings for Python

Download OpenEXR-1.2.0.tar.gz.

OpenEXR is an image format developed by ILM. OpenEXR’s main advantage over other image formats is high dynamic range: it supports floating point pixels. High dynamic range (HDR) images allow a closer representation of real-world colors: shadow and highlight detail is preserved.

Full documentation is online and as a PDF.

Here is a brief sample that duplicates exrnormalize from exrtools

import sys
import array
import OpenEXR
import Imath

if len(sys.argv) != 3:
    print "usage: exrnormalize.py exr-input-file exr-output-file"
    sys.exit(1)

# Open the input file
file = OpenEXR.InputFile(sys.argv[1])

# Compute the size
dw = file.header()['dataWindow']
sz = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)

# Read the three color channels as 32-bit floats
FLOAT = Imath.PixelType(Imath.PixelType.FLOAT)
(R,G,B) = [array.array('f', file.channel(Chan, FLOAT)).tolist() for Chan in ("R", "G", "B") ]

# Normalize so that brightest sample is 1
brightest = max(R + G + B)
R = [ i / brightest for i in R ]
G = [ i / brightest for i in G ]
B = [ i / brightest for i in B ]

# Convert to strings
(Rs, Gs, Bs) = [ array.array('f', Chan).tostring() for Chan in (R, G, B) ]

# Write the three color channels to the output file
out = OpenEXR.OutputFile(sys.argv[2], OpenEXR.Header(sz[0], sz[1]))
out.writePixels({'R' : Rs, 'G' : Gs, 'B' : Gs })

Installing

Prerequisite is Python 2.5 and up, and the OpenEXR C++ library.

  • For a Debian-based Linux:

    sudo apt-get install libopenexr-dev
  • on MacOS with DarwinPorts:

    sudo port install openexr
  • on BSD there is now a port, py-openexr, so all you need to do is:

    cd /usr/ports/graphics/py-openexr
    sudo make install

Then to install the Python bindings themselves:

easy_install -U openexr

or you can download the above source directory then do:

sudo python setup.py install

Change log

OpenEXR-1.2 (Aug 2010)

  • Support for the StringVector attribute, used in multiView.

OpenEXR-1.1

  • efficient multi-channel reading
  • support for Chromaticities type, thanks to Brian Collins of Laika Entertainment

OpenEXR-1.0.3

  • Better error checking, test suite, performance improvements.

OpenEXR-1.0.1

  • Improvements to error messages, argument parsing, and module initialisation, thanks to Campbell Barton.

OpenEXR-1.0.0

  • Code cleanup for better error checking and exception catching
  • Unit test suite
  • Full documentation using Sphinx, with doctests
  • No more memory leaks

OpenEXR-0.0.4

  • Memory deallocation bug fix
  • Exception raising for file open error, thanks to Nick Wolfe from Laika Entertainment

OpenEXR-0.0.3

  • Support for preview images
  • The ‘channel’ method now takes a pixeltype parameter for direct reading of HALF images.
  • Python 2.5 fixes, thanks to Jonathan Stroud of Double Negative.

OpenEXR-0.0.1

  • initial release

Not Yet Implemented

I have yet to implement various OpenEXR features - so if you want something in particular, email me and I will try to get it done.

  • Writing of tiled images
  • Multiresoltion images
  • Some attribute types
  • Nonunity channel sampling frequencies
  • No support for interleaved channel data