This repository contains two Python scripts designed to convert RAW image files into monochrome (black & white) images. One script outputs a DNG file, while the other produces a TIFF file. Both scripts:
- Extract each color channel (R, G1, G2, B) from the Bayer pattern.
- Apply Rec.709 luminance coefficients to generate a single-channel (grayscale) image.
- Multiply the result by a small factor to utilize the 16-bit range effectively.
- Copy relevant metadata from the original RAW file using exiftool.
Note on Compatibility
- DNG Output: The resulting DNG files are recognized correctly in darktable and will allow automatic lens detection and lens corrections. However, Adobe software (Lightroom, Photoshop) currently does not recognize these monochrome DNG files properly.
- TIFF Output: The TIFF files are widely recognized, including by Adobe Lightroom, which will also auto-detect the lens for lens corrections.
- Description: Converts RAW files to a single-channel (monochrome) DNG.
- Key Steps:
- Description: Converts RAW files to a single-channel (monochrome) TIFF.
- Key Steps:
Make sure you have the following installed:
- Python 3.6+ (tested on Python 3.10).
- pip for installing Python packages.
- rawpy (Python RAW image processing library).
- numpy.
- exiftool (external command-line tool for metadata).
- Ensure
exiftoolis accessible in your system’s PATH.
- Ensure
- pidng (required only for the DNG script).
- tifffile (required only for the TIFF script).
You can install the Python dependencies via:
pip install rawpy numpy pidng tifffileNote: If you do not need to convert to DNG, you may skip installing pidng. If you do not need TIFF output, you can skip tifffile.
-
Clone this repository or download the two script files (
raw2mono_dng.pyandraw2mono_tiff.py). -
Install dependencies as described above.
-
Confirm
exiftoolis installed and available in your PATH. For example:exiftool -ver
This should display the installed version of exiftool.
Both scripts use a straightforward command-line interface with --input and --output arguments.
python raw2mono_dng.py --input path/to/input.raw --output path/to/output.dngParameters:
--input: The path to your source RAW file.--output: The path (and filename) for the resulting DNG file.
Example:
python raw2mono_dng.py --input IMG_0001.NEF --output IMG_0001_monochrome.dngOnce completed, the script will:
- Read the RAW file (
IMG_0001.NEF). - Convert it to a single-channel grayscale image using the Rec.709 coefficients.
- Save it as
IMG_0001_monochrome.dng. - Copy metadata from the original file via
exiftool.
python raw2mono_tiff.py --input path/to/input.raw --output path/to/output.tiffParameters:
--input: The path to your source RAW file.--output: The path (and filename) for the resulting TIFF file.
Example:
python raw2mono_tiff.py --input IMG_0001.CR2 --output IMG_0001_monochrome.tiffOnce completed, the script will:
- Read the RAW file (
IMG_0001.CR2). - Convert it to a single-channel grayscale image using the Rec.709 coefficients.
- Save it as
IMG_0001_monochrome.tiff. - Copy metadata from the original file via
exiftool.
-
Reading RAW:
- We use rawpy to decode the RAW file into a numpy array (
raw_data). - This array represents the sensor’s Bayer pattern data.
- We use rawpy to decode the RAW file into a numpy array (
-
Extracting Channels:
- We slice the raw mosaic into the four color channels: R, G1, G2, and B.
- For example,
R = raw_data[0::2, 0::2],B = raw_data[1::2, 1::2], etc.
-
Grayscale Conversion:
- We apply the Rec.709 luminance coefficients:
[ L = R \times 0.2126 + \frac{(G1 + G2)}{2} \times 0.7152 + B \times 0.0722 ] - This produces a single-channel float array.
- We apply the Rec.709 luminance coefficients:
-
16-Bit Scaling:
- The float array is multiplied by 4 (arbitrary scaling to better use the 16-bit range).
- The result is converted to 16-bit integers (
np.uint16).
-
Writing to DNG or TIFF:
-
Metadata Copy:
- Finally, we use
exiftoolto copy all tags from the original RAW into the new DNG or TIFF. This preserves capture time, camera make/model, and other relevant EXIF data.
- Finally, we use