forked from amonod/hdrplus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_dngs_to_png.py
37 lines (35 loc) · 1.75 KB
/
all_dngs_to_png.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
import os
import glob
import rawpy
import cv2
if __name__ == "__main__":
burstFolder = r'path/to/input_folder'
outputFolder = r'path/to/output_folder'
extension = '.png' # replace this to '.jpg' if preferred
rawpyArgs = {
'demosaic_algorithm' : rawpy.DemosaicAlgorithm.DHT, # replace by rawpy.DemosaicAlgorithm.LINEAR for bilinear interpolation
'half_size' : False,
'use_camera_wb' : True, # apply white balance with coefficients saved at capture time
'use_auto_wb' : False,
'no_auto_bright': True, # set this to False if you want image brightness to be automatically changed (not recommended)
'output_color' : rawpy.ColorSpace.sRGB,
'gamma': (2.4, 12.92), # approximates the sRGB gamma compression function
'output_bps' : 8
}
# Get the list of all .dng images in the burst path
rawPathList = glob.glob(os.path.join(burstFolder, '*.dng'))
# sort the raw list in ascending order to avoid errors in reference frame selection
rawPathList.sort()
assert rawPathList != [], 'At least one raw .dng file must be present in the burst folder.'
# get a minimally processed 8 bit sRGB version of each .dng image
for i, rawPath in enumerate(rawPathList):
print("processing image {}/{}: {}".format(i + 1, len(rawPathList), rawPath))
with rawpy.imread(rawPath) as rawObject:
# get the processed 8bit sRGB matrix
outputImage = rawObject.postprocess(**rawpyArgs)
outputName = os.path.join(outputFolder, os.path.basename(rawPath)[:-4] + extension)
# use whatever library you want (cv2, imageio, pillow/PIL) to save the image
if extension == '.png':
cv2.imwrite(outputName, cv2.cvtColor(outputImage, cv2.COLOR_RGB2BGR))
elif extension == '.jpg':
cv2.imwrite(outputName, cv2.cvtColor(outputImage, cv2.COLOR_RGB2BGR), [cv2.IMWRITE_JPEG_QUALITY, 100])