Colour - HDRI - Examples: Merge from Raw Files using Rawpy

Through this example, some Canon EOS 5D Mark II CR2 files will be merged together in order to create a single radiance image.

The following steps will be taken:

  • Creation of an image stack using CR2 files:
    • Reading of the CR2 files Exif metadata using Phil Harvey's ExifTool.
    • Reading of the CR2 files pixel data using rawpy.
  • Merging of the image stack into a radiance image.
  • Display of the final resulting radiance image.

Raw Files Filtering


In [1]:
import logging
import os
import rawpy

import colour
from colour.plotting import *

from colour_hdri import (
    EXAMPLES_RESOURCES_DIRECTORY,
    Image,
    ImageStack,
    filter_files,
    image_stack_to_radiance_image,
    weighting_function_Debevec1997)
from colour_hdri.plotting import plot_radiance_image_strip

logging.basicConfig(level=logging.INFO)

RESOURCES_DIRECTORY = os.path.join(EXAMPLES_RESOURCES_DIRECTORY,
                                   'frobisher_001')

colour.utilities.filter_warnings()

colour.utilities.describe_environment();


===============================================================================
*                                                                             *
*   Interpreter :                                                             *
*       python : 3.7.4 (default, Sep  7 2019, 18:27:02)                       *
*                [Clang 10.0.1 (clang-1001.0.46.4)]                           *
*                                                                             *
*   colour-science.org :                                                      *
*       colour : 0.3.14                                                       *
*       colour-hdri : v0.1.5-60-g9648d86                                      *
*                                                                             *
*   Runtime :                                                                 *
*       imageio : 2.6.1                                                       *
*       matplotlib : 3.0.3                                                    *
*       numpy : 1.17.3                                                        *
*       scipy : 1.3.1                                                         *
*       six : 1.12.0                                                          *
*       recordclass : 0.12.0.1                                                *
*                                                                             *
===============================================================================

In [2]:
colour_style();

In [3]:
RAW_FILES = filter_files(RESOURCES_DIRECTORY, ('CR2',))

In [4]:
def read_raw_file(path):
    raw = rawpy.imread(path).postprocess(gamma=(1, 1), 
                                         no_auto_bright=True, 
                                         output_bps=16)
    return raw.astype(float) / 65535.
    
    
plot_image(colour.cctf_encoding(
            read_raw_file(RAW_FILES[-2])[1250:2250, 3000:4000, ...]),
           {'text': os.path.basename(RAW_FILES[-2])});


Radiance Image Merge


In [5]:
def merge_from_raw_files_using_rawpy(
        raw_files,
        output_directory,
        batch_size=5,
        weighting_function=weighting_function_Debevec1997):
    paths = []
    for raw_files in colour.utilities.batch(raw_files, batch_size):
        image_stack = ImageStack()
        for raw_file in raw_files:
            image = Image(raw_file)
            image.read_metadata()
            image.data = read_raw_file(raw_file)
            image_stack.append(image)

        path = os.path.join(
            output_directory,
            '{0}_{1}_MRFUR.{2}'.format(
                os.path.splitext(os.path.basename(image_stack.path[0]))[0],
                batch_size,
                'exr'))
        paths.append(path)

        logging.info('Merging "{0}"...'.format(path))
        logging.info('\tImage stack "F Number" (Exif): {0}'.format(
            image_stack.f_number))
        logging.info('\tImage stack "Exposure Time" (Exif): {0}'.format(
            image_stack.exposure_time))
        logging.info('\tImage stack "ISO" (Exif): {0}'.format(
            image_stack.iso))
        image = image_stack_to_radiance_image(image_stack,
                                              weighting_function,
                                              weighting_average=True)

        logging.info('Writing "{0}"...'.format(path))
        colour.write_image(image, path)

    return paths


PATHS = merge_from_raw_files_using_rawpy(
    RAW_FILES, RESOURCES_DIRECTORY)


INFO:root:Reading "/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2598.CR2" image metadata.
INFO:root:Reading '/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2598.CR2' image exif data.
INFO:root:Reading "/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2599.CR2" image metadata.
INFO:root:Reading '/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2599.CR2' image exif data.
INFO:root:Reading "/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2600.CR2" image metadata.
INFO:root:Reading '/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2600.CR2' image exif data.
INFO:root:Reading "/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2601.CR2" image metadata.
INFO:root:Reading '/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2601.CR2' image exif data.
INFO:root:Reading "/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2602.CR2" image metadata.
INFO:root:Reading '/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2602.CR2' image exif data.
INFO:root:Merging "/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2598_5_MRFUR.exr"...
INFO:root:	Image stack "F Number" (Exif): [ 8.  8.  8.  8.  8.]
INFO:root:	Image stack "Exposure Time" (Exif): [  2.00000000e-03   1.66666667e-02   1.25000000e-01   1.00000000e+00
   8.00000000e+00]
INFO:root:	Image stack "ISO" (Exif): [ 100.  100.  100.  100.  100.]
INFO:root:Writing "/Users/kelsolaar/Documents/Development/colour-science/colour-hdri/colour_hdri/resources/colour-hdri-examples-datasets/frobisher_001/IMG_2598_5_MRFUR.exr"...

Radiance Image Display


In [6]:
plot_radiance_image_strip(colour.read_image(PATHS[0]));