-
Notifications
You must be signed in to change notification settings - Fork 35
/
exr2jpg.py
26 lines (22 loc) · 840 Bytes
/
exr2jpg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import OpenEXR
import Imath
import Image
import sys
def main(exrfile, jpgfile):
file = OpenEXR.InputFile(exrfile)
pt = Imath.PixelType(Imath.PixelType.FLOAT)
dw = file.header()['dataWindow']
size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
rgbf = [Image.fromstring("F", size, file.channel(c, pt)) for c in "RGB"]
extrema = [im.getextrema() for im in rgbf]
darkest = min([lo for (lo,hi) in extrema])
lighest = max([hi for (lo,hi) in extrema])
scale = 255 / (lighest - darkest)
def normalize_0_255(v):
return (v * scale) + darkest
rgb8 = [im.point(normalize_0_255).convert("L") for im in rgbf]
Image.merge("RGB", rgb8).save(jpgfile)
if __name__ == "__main__":
if len(sys.argv) != 3:
print "usage: exr2jpg <exrfile> <jpgfile>"
main(sys.argv[1], sys.argv[2])