-
Notifications
You must be signed in to change notification settings - Fork 1
/
cnv_jpeg_to_dng.py
executable file
·78 lines (64 loc) · 3.19 KB
/
cnv_jpeg_to_dng.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
#!/usr/bin/env python3
import os
import sys
import platform
import numpy as np
import tifffile as TIFF
import pyexiv2
from turbojpeg import TurboJPEG, TJPF_RGB
import argparse
from xphase_data import XphaseTransforms
#fugly, find a better solution for generating RATIONAL/SRATIONAL
def cm_to_flatrational(input_array):
retarray = np.ones(input_array.size*2, dtype=np.int32)
retarray[0::2] = (input_array.flatten()*10000).astype(np.int32)
retarray[1::2] = 10000
return retarray
def write_dng(jpegdata, exifdata, filebase):
dll_names = {'Windows' : 'libturbojpeg.dll',
'Linux' : 'libturbojpeg.so'}
def init_TurboJPEG():
if(hasattr(sys, 'frozen')):
#Running under PyInstaller
if(getattr(sys, 'frozen') == True):
dll_path = os.path.join(getattr(sys, '_MEIPASS'), 'turbojpeg.libs', dll_names[platform.system()])
return TurboJPEG(dll_path)
else:
exit('Running under PyInstaller single-file mode not yet supported')
else:
return TurboJPEG()
xt = XphaseTransforms()
jpeg = init_TurboJPEG()
#DNG color matrix as SRATIONAL, pulled from an PanoManager DNG
dng_color_matrix = [2147483647, 1268696091, -1208266623, 2147483647, -180777967, 2147483647,
-826519231, 2147483647, 2147483647, 1937550026, 683803903, 2147483647,
-128558463, 2147483647, 411880927, 2147483647, 2147483647, 2046508879]
dngname = filebase + '.dng'
rgbdata = jpeg.decode(jpegdata, pixel_format=TJPF_RGB)
# It seems like every lens except 00, 01, 23, 24 are flipped upside down by PanoManager
(garbage, lensnum, shotnum) = filebase.split("_",3)
if int(lensnum) not in [0, 1, 23, 24] and exifdata['Exif.Image.Model'] != 'Scan':
rgbdata = np.fliplr(np.flipud(rgbdata))
#Colorspace and transfer function conversion
rawdata = xt.jpeg_to_raw(rgbdata).astype(np.uint16)
dng_extratags = []
dng_extratags.append(('ColorMatrix1', '2i', 9, dng_color_matrix))
dng_extratags.append(('CalibrationIlluminant1', 'H', 1, 23)) #is there an enum for this in tifffile??? 23 = D50
dng_extratags.append(('BlackLevel', 'H', 1, 0)) #BlackLevel - We subtracted black during processing, so it is 0
dng_extratags.append(('WhiteLevel', 'I', 1, 65281)) #WhiteLevel
dng_extratags.append(('DNGVersion', 'B', 4, [1,4,0,0])) #DNGVersion
dng_extratags.append(('DNGBackwardVersion', 'B', 4, [1,4,0,0])) #DNGBackwardVersion
dng_extratags.append(('AsShotNeutral', '2I', 3, [1, 1, 1, 1, 1, 1])) #Xphase pre-applies a D50 white balance so AsShotNeutral is 1.0, 1.0, 1.0 """
with TIFF.TiffWriter(dngname) as dng:
dng.write(rawdata,
photometric=34892, #tiffile does not have enums for LinearRaw, use numeric instead
extratags=dng_extratags)
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--input', required=True,
help='path to input file')
args = vars(ap.parse_args())
jpeg_file = args['input']
filebase = os.path.splitext(jpeg_file)[0]
with open(jpeg_file, 'rb') as jpgfile:
write_dng(jpgfile.read(), {}, filebase)