1
+
2
+
3
+ ## Convert images into npys and normalize them in range [0,1]
4
+
5
+ from __future__ import division
6
+ import os
7
+ import numpy as np
8
+ import rawpy
9
+ from glob import glob
10
+ from tqdm import tqdm
11
+ import lycon
12
+ from natsort import natsorted
13
+
14
+ from joblib import Parallel , delayed
15
+ import multiprocessing
16
+
17
+ dir_ = './fivek_dataset/'
18
+ img_type = 'jpg' # change 'jpg' to 'raw' for converting raw images to npys
19
+
20
+ if img_type == 'raw' :
21
+ input_dir = dir_ + 'RAW'
22
+ output_dir = dir_ + 'RAW_npy'
23
+ files = glob (input_dir + '/*.NEF' ) + glob (input_dir + '/*.DNG' ) + glob (input_dir + '/*.dng' )
24
+
25
+ if img_type == 'jpg' :
26
+ input_dir = dir_ + 'RGB_jpg'
27
+ output_dir = dir_ + 'RGB_npy'
28
+ files = glob (input_dir + '/*.jpg' )
29
+
30
+ os .makedirs (output_dir , exist_ok = True )
31
+
32
+ files = natsorted (files )
33
+
34
+ def bit_depth (x ):
35
+ return np .ceil (np .log (x .max ())/ np .log (2 ))
36
+
37
+ def raw2npy (inp_path ):
38
+ filename = os .path .splitext (os .path .split (inp_path )[- 1 ])[0 ] + '.npy'
39
+ filepath = os .path .join (output_dir ,filename )
40
+ raw = rawpy .imread (inp_path )
41
+ im_raw = raw .raw_image_visible .astype (np .float32 )
42
+ # normalize values in range [0,1]
43
+ norm_factor = 2 ** bit_depth (im_raw )
44
+ im_raw = im_raw / norm_factor
45
+ im_raw = im_raw [..., np .newaxis ]
46
+ np .save (filepath ,im_raw )
47
+
48
+ def jpg2npy (inp_path ):
49
+ filename = os .path .splitext (os .path .split (inp_path )[- 1 ])[0 ] + '.npy'
50
+ filepath = os .path .join (output_dir ,filename )
51
+ jpg = lycon .load (inp_path )
52
+ im_jpg = jpg .astype (np .float16 )
53
+ # normalize values in range [0,1]
54
+ norm_factor = 255
55
+ im_jpg = im_jpg / norm_factor
56
+ np .save (filepath ,im_jpg )
57
+
58
+ num_cores = multiprocessing .cpu_count ()
59
+
60
+ from concurrent .futures import ProcessPoolExecutor
61
+
62
+ if img_type == 'raw' :
63
+ with ProcessPoolExecutor (num_cores ) as e : e .map (raw2npy , files )
64
+ if img_type == 'jpg' :
65
+ with ProcessPoolExecutor (num_cores ) as e : e .map (jpg2npy , files )
0 commit comments