File size: 1,752 Bytes
b4754db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy
import OpenEXR
import Imath
import imageio
import glob
import os

def exr2hdr(exrpath):
    File = OpenEXR.InputFile(exrpath)
    PixType = Imath.PixelType(Imath.PixelType.FLOAT)
    DW = File.header()['dataWindow']
    CNum = len(File.header()['channels'].keys())
    if (CNum > 1):
    	Channels = ['R', 'G', 'B']
    	CNum = 3
    else:
    	Channels = ['G']
    Size = (DW.max.x - DW.min.x + 1, DW.max.y - DW.min.y + 1)
    Pixels = [numpy.fromstring(File.channel(c, PixType), dtype=numpy.float32) for c in Channels]
    hdr = numpy.zeros((Size[1],Size[0],CNum),dtype=numpy.float32)
    if (CNum == 1):
        hdr[:,:,0] = numpy.reshape(Pixels[0],(Size[1],Size[0]))
    else:
	    hdr[:,:,0] = numpy.reshape(Pixels[0],(Size[1],Size[0]))
	    hdr[:,:,1] = numpy.reshape(Pixels[1],(Size[1],Size[0]))
	    hdr[:,:,2] = numpy.reshape(Pixels[2],(Size[1],Size[0]))
    return hdr

def writehdr(hdrpath,hdr):
	h, w, c = hdr.shape
	if c == 1:
		hdr = numpy.pad(hdr, ((0, 0), (0, 0), (0, 2)), 'constant')
		hdr[:,:,1] = hdr[:,:,0]
		hdr[:,:,2] = hdr[:,:,0]
	imageio.imwrite(hdrpath,hdr,format='hdr')

def load_exr(filename):
	hdr = exr2hdr(filename)
	h, w, c = hdr.shape
	if c == 1:
		hdr = numpy.squeeze(hdr)
	return hdr


def test_exr():
	files = glob.glob('D:/MLProjects/data/home/*.exr')
	savepath = 'D:/MLProjects/data/home'
	total = len(files)
	count = 0
	print ('Files Num:', total)	
	for file in files:
	    hdr = exr2hdr(file)
	    filename,file_ext = os.path.splitext(file)
	    filename = os.path.basename(filename)
	    filename = filename + '.hdr'
	    curpath = os.path.join(savepath,filename)
	    writehdr(curpath,hdr)
	    count = count + 1
	    print ('process:', count, '/', total)

if __name__ == '__main__':
    test_exr()