Skip to content

Commit

Permalink
fix example (#1714)
Browse files Browse the repository at this point in the history
and raise error when running the buggy version
instead of python mysteriously crashing

fixes #1711

Signed-off-by: Heshy Roskes <[email protected]>
  • Loading branch information
hroskes authored Jul 24, 2024
1 parent da9aac0 commit 4b3bcf3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/wrappers/python/Imath.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,18 @@ class Channel:
>>> import Imath
>>> print Imath.Channel(Imath.PixelType(Imath.PixelType.FLOAT), 4, 4)
FLOAT (4, 4)
>>> print Imath.Channel(Imath.PixelType.FLOAT, 4, 4)
Traceback (most recent call last):
...
TypeError: type needs to be a PixelType.
"""

def __init__(self, type = PixelType(PixelType.HALF), xSampling = 1, ySampling = 1):
self.type = type
self.xSampling = xSampling
self.ySampling = ySampling
if not isinstance(self.type, PixelType):
raise TypeError("type needs to be a PixelType.")
def __repr__(self):
return repr(self.type) + " " + repr((self.xSampling, self.ySampling))
def __eq__(self, other):
Expand Down
11 changes: 6 additions & 5 deletions src/wrappers/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ for more information.

The "hello, world" image writer:

import OpenEXR
import OpenEXR, Imath
from array import array

width = 10
height = 10
size = width * height

h = OpenEXR.Header(width,height)
h['channels'] = {'R' : Imath.Channel(FLOAT),
'G' : Imath.Channel(FLOAT),
'B' : Imath.Channel(FLOAT),
'A' : Imath.Channel(FLOAT)}
h['channels'] = {'R' : Imath.Channel(Imath.PixelType(Imath.PixelType.FLOAT)),
'G' : Imath.Channel(Imath.PixelType(Imath.PixelType.FLOAT)),
'B' : Imath.Channel(Imath.PixelType(Imath.PixelType.FLOAT)),
'A' : Imath.Channel(Imath.PixelType(Imath.PixelType.FLOAT))}
o = OpenEXR.OutputFile("hello.exr", h)
r = array('f', [n for n in range(size*0,size*1)]).tobytes()
g = array('f', [n for n in range(size*1,size*2)]).tobytes()
Expand Down
7 changes: 7 additions & 0 deletions src/wrappers/python/tests/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ def test_invalid_pixeltype():
else:
assert 0

try:
Imath.Channel(FLOAT)
except:
pass
else:
assert 0

print("invalid pixeltype ok")

testList.append(("test_invalid_pixeltype", test_invalid_pixeltype))
Expand Down

0 comments on commit 4b3bcf3

Please sign in to comment.