Download OpenEXR-0.0.4.tar.gz
OpenEXR is an image format developed by ILM. Its main advantage is higher dynamic range: it supports floating point pixels.
This small module adds Python bindings for the OpenEXR libraries: you can now read and write OpenEXR files from Python. Note that this module only loads and stores images: it does not do any image manipulation operations. For that you might want to use one of:
Here's a brief sample that duplicates exrnormalize from exrtools.
import sys
import OpenEXR
import Image
from Imath import *
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 monochrome float format images
(R,G,B) = [Image.fromstring("F", sz, file.channel(Chan)) for Chan in "RGB"]
file.close()
# Normalize so that brightest sample is 1
hi = max([Chan.getextrema()[1] for Chan in [R,G,B]])
(R,G,B) = [Chan.point(lambda i: i * (1. / hi) + 0.0) for Chan in [R,G,B]]
# Convert the three images into strings
(R,G,B) = [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':R, 'G':G, 'B':B})
out.close()
|
InputFile objects support the following methods:
Imath.PixelType(OpenEXR.FLOAT) by default. If miny and maxy are not supplied, then the method reads the entire image. Note that this method returns the channel as a Python string: the caller must then convert it to the appropriate format as necessary.
OutputFile objects support the following methods:
This module represents OpenEXR headers as regular Python dictionaries. In this dictionary the keys are strings, and the values are such that the module can determine their type. The module Imath provides the classes for attribute types.
>>> import Imath
>>> import OpenEXR
>>> print OpenEXR.InputFile("GoldenGate.exr").header()
{'capDate': '2004:01:04 18:10:00',
'compression': PIZ_COMPRESSION,
'latitude': 37.827701568603516,
'pixelAspectRatio': 1.0,
'altitude': 274.5,
'displayWindow': (0, 0) - (1261, 859),
'focus': inf,
'comments': 'View from Hawk Hill towards San Francisco',
'screenWindowWidth': 1.1499999761581421,
'channels': {'R': HALF (1, 1), 'B': HALF (1, 1), 'G': HALF (1, 1)},
'isoSpeed': 50.0,
'utcOffset': 28800.0,
'longitude': -122.49960327148438,
'dataWindow': (0, 0) - (1261, 859),
'screenWindowCenter': (0.0, 0.0),
'aperture': 2.7999999523162842,
'preview': <Imath.PreviewImage instance at 0x81b8c6c>
'owner': 'Copyright 2004 Industrial Light & Magic',
'expTime': 8.0,
'lineOrder': INCREASING_Y}
|
A value may be:
header['owner'] = 'Copyright 2007 James Bowman' |
header['isoSpeed'] = 50.0 |
header['version'] = 1001 |
header['channels'] = { 'L' : Imath.Channel(PixelType(OpenEXR.HALF)), 'Z' : Imath.Channel(PixelType(OpenEXR.FLOAT))}
|
header['dataWindow'] = Imath.Box2i(Imath.point(0,0), Imath.point(640,480)) |
header['regionOfInterest'] = Imath.Box2f(Imath.point(75.0,75.0), Imath.point(100.0,100.0)) |
header['originMarker'] = Imath.point(0.378, 0.878) |
header['lineOrder'] = Imath.LineOrder(Imath.LineOrder.INCREASING_Y) |
header['preview'] = Imath.PreviewImage(320,200,pixels) |
import Image
im = ...
header['preview'] = Imath.PreviewImage(im.size[0], im.size[1], im.convert("RGBA").tostring())
|
header['Compression'] = Imath.Compression(Imath.Compression.PIZ_COMPRESSION) |
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.