-
Notifications
You must be signed in to change notification settings - Fork 3
/
img_io.py
138 lines (112 loc) · 4.81 KB
/
img_io.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
" License:
" -----------------------------------------------------------------------------
" Copyright (c) 2017, Gabriel Eilertsen.
" All rights reserved.
"
" Redistribution and use in source and binary forms, with or without
" modification, are permitted provided that the following conditions are met:
"
" 1. Redistributions of source code must retain the above copyright notice,
" this list of conditions and the following disclaimer.
"
" 2. Redistributions in binary form must reproduce the above copyright notice,
" this list of conditions and the following disclaimer in the documentation
" and/or other materials provided with the distribution.
"
" 3. Neither the name of the copyright holder nor the names of its contributors
" may be used to endorse or promote products derived from this software
" without specific prior written permission.
"
" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
" POSSIBILITY OF SUCH DAMAGE.
" -----------------------------------------------------------------------------
"
" Description: Image I/O, for both LDR and HDR images.
" Author: Gabriel Eilertsen, gabriel.eilertsen@liu.se
" Date: Aug 2017
"""
import numpy as np
import scipy.misc
import OpenEXR, Imath
class IOException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
# Read and prepare 8-bit image in a specified resolution
def readLDR(file, sz, clip=True, sc=1.0):
try:
x_buffer = scipy.misc.imread(file)
# Clip image, so that ratio is not changed by image resize
if clip:
sz_in = [float(x) for x in x_buffer.shape]
sz_out = [float(x) for x in sz]
r_in = sz_in[1]/sz_in[0]
r_out = sz_out[1]/sz_out[0]
if r_out / r_in > 1.0:
sx = sz_in[1]
sy = sx/r_out
else:
sy = sz_in[0]
sx = sy*r_out
yo = np.maximum(0.0, (sz_in[0]-sy)/2.0)
xo = np.maximum(0.0, (sz_in[1]-sx)/2.0)
x_buffer = x_buffer[int(yo):int(yo+sy),int(xo):int(xo+sx),:]
# Image resize and conversion to float
x_buffer = scipy.misc.imresize(x_buffer, sz)
x_buffer = x_buffer.astype(np.float32)/255.0
# Scaling and clipping
if sc > 1.0:
x_buffer = np.minimum(1.0, sc*x_buffer)
x_buffer = x_buffer[np.newaxis,:,:,:]
return x_buffer
except Exception as e:
raise IOException("Failed reading LDR image: %s"%e)
# Write exposure compensated 8-bit image
def writeLDR(img, file, exposure=0):
# Convert exposure fstop in linear domain to scaling factor on display values
sc = np.power(np.power(2.0, exposure), 0.5)
try:
scipy.misc.toimage(sc*np.squeeze(img), cmin=0.0, cmax=1.0).save(file)
except Exception as e:
raise IOException("Failed writing LDR image: %s"%e)
# Write HDR image using OpenEXR
def writeEXR(img, file):
try:
img = np.squeeze(img)
sz = img.shape
header = OpenEXR.Header(sz[1], sz[0])
half_chan = Imath.Channel(Imath.PixelType(Imath.PixelType.HALF))
header['channels'] = dict([(c, half_chan) for c in "RGB"])
out = OpenEXR.OutputFile(file, header)
R = (img[:,:,0]).astype(np.float16).tostring()
G = (img[:,:,1]).astype(np.float16).tostring()
B = (img[:,:,2]).astype(np.float16).tostring()
out.writePixels({'R' : R, 'G' : G, 'B' : B})
out.close()
except Exception as e:
raise IOException("Failed writing EXR: %s"%e)
# Read training data (HDR ground truth and LDR JPEG images)
def load_training_pair(name_hdr, name_jpg):
data = np.fromfile(name_hdr, dtype=np.float32)
ss = len(data)
if ss < 3:
return (False,0,0)
sz = np.floor(data[0:3]).astype(int)
npix = sz[0]*sz[1]*sz[2]
meta_length = ss - npix
# Read binary HDR ground truth
y = np.reshape(data[meta_length:meta_length+npix], (sz[0], sz[1], sz[2]))
# Read JPEG LDR image
x = scipy.misc.imread(name_jpg).astype(np.float32)/255.0
return (True,x,y)