Through this example, some Canon EOS 5D Mark II CR2 files will be white balanced using Colour - HDRI Adobe DNG SDK colour processing.
The following steps will be taken:
In [1]:
import logging
import numpy as np
import os
import colour
from colour.plotting import *
from colour_hdri import (
EXAMPLES_RESOURCES_DIRECTORY,
camera_neutral_to_xy,
camera_space_to_XYZ_matrix,
convert_dng_files_to_intermediate_files,
convert_raw_files_to_dng_files,
filter_files,
read_dng_files_exif_tags,
highlights_recovery_LCHab,
update_exif_tags)
from colour_hdri.models import (
ADOBE_DNG_XYZ_ILLUMINANT,
DNG_ILLUMINANTS_CORRELATED_COLOUR_TEMPERATURE,
LIGHT_SOURCE_TAG_TO_DNG_ILLUMINANTS)
logging.basicConfig(level=logging.INFO)
RESOURCES_DIRECTORY = os.path.join(EXAMPLES_RESOURCES_DIRECTORY,
'frobisher_001')
colour.utilities.filter_warnings()
colour.utilities.describe_environment();
In [2]:
colour_style();
In [3]:
RAW_FILES = filter_files(RESOURCES_DIRECTORY, ('CR2',))
DNG_FILES = convert_raw_files_to_dng_files(RAW_FILES, RESOURCES_DIRECTORY)
INTERMEDIATE_FILES = convert_dng_files_to_intermediate_files(
DNG_FILES, RESOURCES_DIRECTORY, demosaicing=True)
update_exif_tags(zip(DNG_FILES, INTERMEDIATE_FILES))
Out[3]:
In [4]:
plot_image(colour.cctf_encoding(colour.read_image(
str(INTERMEDIATE_FILES[-2]))[1250:2250, 3000:4000, ...]),
{'text': os.path.basename(INTERMEDIATE_FILES[-2])});
In [5]:
def process_raw_files(
dng_files,
output_directory):
paths = []
for dng_file in dng_files:
exif_tags = read_dng_files_exif_tags((dng_file, ))[0]
exif_group = exif_tags['EXIF']
CCT_calibration_illuminant_1 = (
DNG_ILLUMINANTS_CORRELATED_COLOUR_TEMPERATURE[
LIGHT_SOURCE_TAG_TO_DNG_ILLUMINANTS[
exif_group['Calibration Illuminant 1']]])
CCT_calibration_illuminant_2 = (
DNG_ILLUMINANTS_CORRELATED_COLOUR_TEMPERATURE[
LIGHT_SOURCE_TAG_TO_DNG_ILLUMINANTS[
exif_group['Calibration Illuminant 2']]])
M_color_matrix_1 = exif_group['Color Matrix 1']
M_color_matrix_2 = exif_group['Color Matrix 2']
M_camera_calibration_1 = exif_group['Camera Calibration 1']
M_camera_calibration_2 = exif_group['Camera Calibration 2']
analog_balance = exif_group['Analog Balance']
M_forward_matrix_1 = exif_group['Forward Matrix 1']
M_forward_matrix_2 = exif_group['Forward Matrix 2']
as_shot_neutral = exif_group['As Shot Neutral']
logging.info('"As Shot Neutral": "{0}"...'.format(as_shot_neutral))
xy = camera_neutral_to_xy(
as_shot_neutral,
CCT_calibration_illuminant_1,
CCT_calibration_illuminant_2,
M_color_matrix_1,
M_color_matrix_2,
M_camera_calibration_1,
M_camera_calibration_2,
analog_balance)
M_camera_space_to_XYZ = camera_space_to_XYZ_matrix(
xy,
CCT_calibration_illuminant_1,
CCT_calibration_illuminant_2,
M_color_matrix_1,
M_color_matrix_2,
M_camera_calibration_1,
M_camera_calibration_2,
analog_balance,
M_forward_matrix_1,
M_forward_matrix_2)
logging.info('"Camera Space to XYZ": "{0}"...'.format(M_camera_space_to_XYZ))
path = str(dng_file.replace('dng', 'tiff'))
image = colour.read_image(path)
image = colour.utilities.dot_vector(M_camera_space_to_XYZ, image)
image = colour.XYZ_to_sRGB(
image, ADOBE_DNG_XYZ_ILLUMINANT, apply_cctf_encoding=False)
path = os.path.join(
output_directory,
'{0}.{1}'.format(
os.path.splitext(os.path.basename(path))[0],
'exr'))
paths.append(path)
logging.info('Recovering highlights...')
# NOTE: In some edge cases, the threshold for highlights
# recovery can be negative because of out of gamut values.
# For such cases you can perform the recovery in a larger
# RGB colourspace such as *ACES 2065-1* or *Xtreme RGB*.
XYZ_t = colour.utilities.dot_vector(M_camera_space_to_XYZ, np.ones(3))
RGB_t = colour.XYZ_to_sRGB(
XYZ_t, ADOBE_DNG_XYZ_ILLUMINANT, apply_cctf_encoding=False)
image = highlights_recovery_LCHab(image, RGB_t[1])
logging.info('Writing "{0}"...'.format(path))
colour.write_image(image, path)
return paths
PATHS = process_raw_files(DNG_FILES, RESOURCES_DIRECTORY)
In [6]:
plot_image(colour.cctf_encoding(colour.read_image(PATHS[-2])));