-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_pfm_as_image.py
executable file
·63 lines (48 loc) · 1.43 KB
/
save_pfm_as_image.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
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
#! /usr/bin/python
import os
import sys
import numpy as np
import iio
import re
def load_pfm(filename):
color = None
width = None
height = None
scale = None
endian = None
file = open(filename, "r")
header = file.readline().rstrip()
if header == 'PF':
color = True
elif header == 'Pf':
color = False
else:
raise Exception('Not a PFM file.')
dim_match = re.match(r'^(\d+)\s(\d+)\s$', file.readline())
if dim_match:
width, height = map(int, dim_match.groups())
else:
raise Exception('Malformed PFM header.')
scale = float(file.readline().rstrip())
if scale < 0: # little-endian
endian = '<'
scale = -scale
else:
endian = '>' # big-endian
data = np.fromfile(file, endian + 'f')
shape = (height, width, 3) if color else (height, width)
return np.reshape(data, shape), scale
#-----------------------------------------------------------------------
if __name__ == '__main__':
disp_map = sys.argv[1]
image, scale = load_pfm(disp_map)
vmin = image.min()
vmax = image.min()
for value in image:
for v in value:
if not np.isinf(v) and v > vmax:
vmax = v
image = 255 - (((image - vmin)/ (vmax-vmin)) * 255)
image = np.flip(image,0)
im = image.astype(np.uint8)
iio.write(disp_map, im)