rawpy

0.26.1 · active · verified Wed Apr 15

rawpy is an easy-to-use Python wrapper for the LibRaw library, providing comprehensive RAW image processing capabilities. It also includes functionality for finding and repairing hot/dead pixels. The library is actively maintained with regular updates, often driven by new LibRaw releases and Python version support. The current version is 0.26.1.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a RAW image, postprocess it using default settings, and save the resulting RGB image as a TIFF file. It also shows an example of saving a 16-bit linear image. It uses `imageio` for saving the output, which needs to be installed separately. The code includes basic error handling for unsupported RAW files and uses a dummy file for execution without requiring an actual RAW image upfront.

import rawpy
import imageio.v3 as iio
import os

# Create a dummy RAW file for demonstration (replace with your .CR2, .NEF, etc.)
# In a real scenario, you would have an actual RAW image file.
dummy_raw_content = b'\x00' * 1024 # Not a real RAW, but allows the code to run without file not found
dummy_raw_filename = 'dummy.nef'
with open(dummy_raw_filename, 'wb') as f:
    f.write(dummy_raw_content)

path = dummy_raw_filename

try:
    # Load a RAW file using the context manager for resource safety
    with rawpy.imread(path) as raw:
        # Postprocess the RAW image to an RGB NumPy array
        # Uses default parameters for demosaicing, white balance, etc.
        rgb = raw.postprocess()

        # Save the postprocessed image using imageio (requires 'imageio' installed)
        output_filename = 'default.tiff'
        iio.imwrite(output_filename, rgb)
        print(f"Image saved to {output_filename}")

        # Example: Save as 16-bit linear image
        rgb_16bit = raw.postprocess(gamma=(1,1), no_auto_bright=True, output_bps=16)
        output_filename_16bit = 'linear.tiff'
        iio.imwrite(output_filename_16bit, rgb_16bit)
        print(f"16-bit linear image saved to {output_filename_16bit}")

except rawpy.LibRawFileUnsupportedError:
    print(f"Error: '{path}' is not a supported RAW file format, or it's corrupted/incomplete.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    # Clean up dummy file
    if os.path.exists(dummy_raw_filename):
        os.remove(dummy_raw_filename)

view raw JSON →