-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiff2dng.py
48 lines (41 loc) · 1.82 KB
/
tiff2dng.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
import click
import numpy as np
import tifffile as tf
from pidng.core import RAW2DNG, DNGTags, Tag
from pidng.defs import (CalibrationIlluminant, DNGVersion, Orientation,
PhotometricInterpretation, PreviewColorSpace)
@click.command()
@click.option('--input', type=str, default='input.tiff', help='Path to linear light 32-bit float TIFF file.')
@click.option('--output', type=str, default='output.dng', help='Path to output 16-bit int DNG file.')
def tiff2dng(input, output):
"""Convert a TIFF file in 32-bit floating point linear light to a DNG file in 16-bit integer sRGB."""
image = tf.imread(input)
image = np.rint(image * np.iinfo(np.uint16).max).astype(np.uint16)
height, width, _ = image.shape
# XYZ to RGB inverse matrix
ccm1 = [[ 3240454, 1000000], [ -1537138, 1000000], [ -498531, 1000000],
[ -969266, 1000000], [ 1876010, 1000000], [ 41556, 1000000],
[ 55643, 1000000], [ -204025, 1000000], [ 1057225, 1000000]]
# set DNG tags.
t = DNGTags()
t.set(Tag.ImageWidth, width)
t.set(Tag.ImageLength, height)
t.set(Tag.TileWidth, width)
t.set(Tag.TileLength, height)
t.set(Tag.Orientation, Orientation.Horizontal)
t.set(Tag.PhotometricInterpretation, PhotometricInterpretation.Linear_Raw)
t.set(Tag.SamplesPerPixel, 3)
t.set(Tag.BitsPerSample, [16,16,16])
t.set(Tag.BlackLevel, 0)
t.set(Tag.WhiteLevel, ((1 << 16) -1) )
t.set(Tag.ColorMatrix1, ccm1)
t.set(Tag.CalibrationIlluminant1, CalibrationIlluminant.D65)
t.set(Tag.DNGVersion, DNGVersion.V1_4)
t.set(Tag.DNGBackwardVersion, DNGVersion.V1_2)
t.set(Tag.PreviewColorSpace, PreviewColorSpace.sRGB)
# save to dng file.
r = RAW2DNG()
r.options(t, path="", compress=False)
r.convert(image, filename=output)
if __name__=='__main__':
tiff2dng()