Spaces:
Build error
Build error
File size: 2,375 Bytes
41c6291 | 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 |
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
# 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) |