|
#!/usr/bin/env python3 |
|
""" |
|
pip install numpy tifffile |
|
""" |
|
|
|
import sys |
|
import json |
|
from pathlib import Path |
|
import numpy as np |
|
import tifffile |
|
|
|
if len(sys.argv) != 2: |
|
print(f'Usage: {sys.argv[0]} file.dng', file=sys.stderr) |
|
print(f'Matching file.json sidecar file must exist') |
|
sys.exit(1) |
|
|
|
raw_path = Path(sys.argv[1]) |
|
json_path = Path(f'{str(raw_path)}.json') |
|
dng_path = Path(f'{str(raw_path)}.dng') |
|
|
|
if not raw_path.exists(): |
|
print(f'File not found: {raw_path}', file=sys.stderr) |
|
sys.exit(1) |
|
|
|
if not json_path.exists(): |
|
print(f'JSON sidecar not found: {json_path}', file=sys.stderr) |
|
sys.exit(1) |
|
|
|
with open(json_path, 'r') as f: |
|
metadata = json.load(f) |
|
|
|
width = metadata['width'] |
|
height = metadata['height'] |
|
gain = metadata.get('gain', 16) |
|
exposure_lines = metadata.get('exposure_lines', 18) |
|
|
|
print(f'Dimensions: {width}x{height}') |
|
print(f'Gain: {gain}') |
|
print(f'Exposure lines: {exposure_lines}') |
|
|
|
with open(raw_path, 'rb') as f: |
|
raw_data = f.read() |
|
|
|
bayer_8bit = np.frombuffer(raw_data, dtype=np.uint8) \ |
|
.reshape((height, width)) |
|
|
|
bayer_16bit = (bayer_8bit.astype(np.uint16) << 8) |
|
|
|
with tifffile.TiffWriter(dng_path, bigtiff=False) as tif: |
|
tif.write( |
|
bayer_16bit, |
|
photometric=32803, |
|
compression=1, |
|
planarconfig=1, |
|
bitspersample=16, |
|
rowsperstrip=height, |
|
metadata=None, |
|
extratags=[ |
|
(254, 'I', 1, 0, True), # NewSubFileType |
|
|
|
(271, 's', 0, 'Flashback', True), # Make |
|
(272, 's', 0, 'ONE35', True), # Model |
|
(274, 'H', 1, 1, True), # Orientation |
|
|
|
(50706, 'B', 4, b'\x01\x04\x00\x00', True), # DNGVersion |
|
(50707, 'B', 4, b'\x01\x01\x00\x00', True), # DNGBackwardVersion |
|
(50708, 's', 0, "Flashback ONE35", True), # UniqueCameraModel |
|
(50709, 's', 0, "Flashback ONE35", True), # LocalizedCameraModel |
|
|
|
(50714, 'H', 1, 0, True), # BlackLevel |
|
(50717, 'H', 1, 65535, True), # WhiteLevel |
|
(50718, 'I', 2, [1, 1], True), # DefaultScale |
|
(50780, 'I', 2, [1, 1], True), # BestQualityScale |
|
|
|
(50721, 'i', 9, [10000, 0, 0, 0, 10000, 0, 0, 0, 10000], True), # ColorMatrix1 |
|
(50778, 'H', 1, 21, True), # CalibrationIlluminant1 |
|
|
|
(50829, 'I', 4, [0, 0, height, width], True), # ActiveArea |
|
|
|
(33421, 'H', 2, [2, 2], True), # CFARepeatPatternDim |
|
(33422, 'B', 4, [2, 1, 1, 0], True), # CFAPattern |
|
(50710, 'B', 3, [0, 1, 2], True), # CFAPlaneColor |
|
(50711, 'H', 1, 1, True), # CFALayout |
|
] |
|
) |