rawpy
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
- breaking macOS Intel support was removed in v0.26.0. Users on macOS with Intel CPUs will not be able to install or use rawpy versions 0.26.0 and later.
- breaking As of v0.23.0, rawpy will now raise an error for corrupt or malformed RAW files instead of silently returning garbage data. This improves robustness but may require updating existing code that implicitly handled such cases.
- breaking Python 3.8 support was dropped in v0.22.0. Users on Python 3.8 must upgrade their Python version to continue using recent rawpy releases.
- breaking Numpy 2.x support was added in v0.22.0, but older numpy versions are no longer supported. Ensure your numpy installation is compatible with rawpy.
- gotcha On Linux, `rawpy` requires the `LibRaw` library to be installed system-wide. Binary wheels for `rawpy` do not bundle `LibRaw` on Linux. Installation instructions for `LibRaw` vary by distribution.
- gotcha Attempting to open a non-RAW file, a corrupted file, or a RAW file with an unsupported format/camera model will raise a `rawpy.LibRawFileUnsupportedError`.
- gotcha When using Python's `multiprocessing` module on Linux, deadlocks can occur due to an interaction between OpenMP (used in rawpy's Linux wheels) and the default `fork` start method.
Install
-
pip install rawpy -
pip install --pre rawpy
Imports
- rawpy
import rawpy
- rawpy.enhance
import rawpy.enhance
Quickstart
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)