RawPy class

class rawpy.RawPy

Load RAW images, work on their data, and create a postprocessed (demosaiced) image.

All operations are implemented using numpy arrays.

close(self)

Release all resources and close the RAW image.

Consider using context managers for the same effect:

with rawpy.imread('image.nef') as raw:
  # work with raw object
extract_thumb(self)

Extracts and returns the thumbnail/preview image (whichever is bigger) of the opened RAW image as rawpy.Thumbnail object. For JPEG thumbnails, data is a bytes object and can be written as-is to file. For bitmap thumbnails, data is an ndarray of shape (h,w,c). If no image exists or the format is unsupported, an exception is raised.

with rawpy.imread('image.nef') as raw:
  try:
    thumb = raw.extract_thumb()
  except rawpy.LibRawNoThumbnailError:
    print('no thumbnail found')
  except rawpy.LibRawUnsupportedThumbnailError:
    print('unsupported thumbnail')
  else:
    if thumb.format == rawpy.ThumbFormat.JPEG:
      with open('thumb.jpg', 'wb') as f:
        f.write(thumb.data)
    elif thumb.format == rawpy.ThumbFormat.BITMAP:
      imageio.imsave('thumb.tiff', thumb.data)
Return type

rawpy.Thumbnail

postprocess(self, params=None, **kw)

Postprocess the currently loaded RAW image and return the new resulting image as numpy array.

Parameters
  • params (rawpy.Params) – The parameters to use for postprocessing.

  • **kw

    Alternative way to provide postprocessing parameters. The keywords are used to construct a rawpy.Params instance. If keywords are given, then params must be omitted.

Return type

ndarray of shape (h,w,c)

raw_color(self, int row, int column) → int

Return color index for the given coordinates relative to the full RAW size. Only usable for flat RAW images (see raw_type property).

raw_value(self, int row, int column) → ushort

Return RAW value at given position relative to the full RAW image. Only usable for flat RAW images (see raw_type property).

raw_value_visible(self, int row, int column) → ushort

Return RAW value at given position relative to visible area of image. Only usable for flat RAW images (see raw_type property).

black_level_per_channel

Per-channel black level correction.

Return type

list of length 4

camera_white_level_per_channel

Per-channel saturation levels read from raw file metadata, if it exists. Otherwise None.

Return type

list of length 4, or None if metadata missing

camera_whitebalance

White balance coefficients (as shot). Either read from file or calculated.

Return type

list of length 4

color_desc

String description of colors numbered from 0 to 3 (RGBG,RGBE,GMCY, or GBTG). Note that same letters may not refer strictly to the same color. There are cameras with two different greens for example.

color_matrix

Color matrix, read from file for some cameras, calculated for others.

Return type

ndarray of shape (3,4)

daylight_whitebalance

White balance coefficients for daylight (daylight balance). Either read from file, or calculated on the basis of file data, or taken from hardcoded constants.

Return type

list of length 4

num_colors

Number of colors. Note that e.g. for RGBG this can be 3 or 4, depending on the camera model, as some use two different greens.

raw_colors

An array of color indices for each pixel in the RAW image. Equivalent to calling raw_color(y,x) for each pixel. Only usable for flat RAW images (see raw_type property).

Return type

ndarray of shape (h,w)

raw_colors_visible

Like raw_colors but without margin.

Return type

ndarray of shape (hv,wv)

raw_image

View of RAW image. Includes margin.

For Bayer images, a 2D ndarray is returned. For Foveon and other RGB-type images, a 3D ndarray is returned. Note that there may be 4 color channels, where the 4th channel can be blank (zeros).

Modifying the returned array directly influences the result of calling postprocess().

Warning

The returned numpy array can only be accessed while this RawPy instance is not closed yet, that is, within a with block or before calling close(). If you need to work on the array after closing the RawPy instance, make sure to create a copy of it with raw_image = raw.raw_image.copy().

Return type

ndarray of shape (h,w[,c])

raw_image_visible

Like raw_image but without margin.

Return type

ndarray of shape (hv,wv[,c])

raw_pattern

The smallest possible Bayer pattern of this image.

Return type

ndarray, or None if not a flat RAW image

raw_type

Return the RAW type.

Return type

rawpy.RawType

rgb_xyz_matrix

Camera RGB - XYZ conversion matrix. This matrix is constant (different for different models). Last row is zero for RGB cameras and non-zero for different color models (CMYG and so on).

Return type

ndarray of shape (4,3)

sizes

Return a rawpy.ImageSizes instance with size information of the RAW image and postprocessed image.

tone_curve

Camera tone curve, read from file for Nikon, Sony and some other cameras.

Return type

ndarray of length 65536

white_level

Level at which the raw pixel value is considered to be saturated.

Additional low-level methods:

class rawpy.RawPy

Load RAW images, work on their data, and create a postprocessed (demosaiced) image.

All operations are implemented using numpy arrays.

dcraw_make_mem_image(self)

Return the postprocessed image (see dcraw_process()) as numpy array.

Note

This is a low-level method, consider using postprocess() instead.

Return type

ndarray of shape (h,w,c)

dcraw_make_mem_thumb(self)

Return the thumbnail/preview image (see unpack_thumb()) as rawpy.Thumbnail object. For JPEG thumbnails, data is a bytes object and can be written as-is to file. For bitmap thumbnails, data is an ndarray of shape (h,w,c). If no image exists or the format is unsupported, an exception is raised.

Note

This is a low-level method, consider using extract_thumb() instead.

Return type

rawpy.Thumbnail

dcraw_process(self, params=None, **kw)

Postprocess the currently loaded RAW image.

Note

This is a low-level method, consider using postprocess() instead.

Parameters
  • params (rawpy.Params) – The parameters to use for postprocessing.

  • **kw

    Alternative way to provide postprocessing parameters. The keywords are used to construct a rawpy.Params instance. If keywords are given, then params must be omitted.

open_buffer(self, fileobj)

Opens the given RAW image file-like object. Should be followed by a call to unpack().

Note

This is a low-level method, consider using rawpy.imread() instead.

Parameters

fileobj (file) – The file-like object.

open_file(self, path)

Opens the given RAW image file. Should be followed by a call to unpack().

Note

This is a low-level method, consider using rawpy.imread() instead.

Parameters

path (str) – The path to the RAW image.

unpack(self)

Unpacks/decodes the opened RAW image.

Note

This is a low-level method, consider using rawpy.imread() instead.

unpack_thumb(self)

Unpacks/decodes the thumbnail/preview image, whichever is bigger.

Note

This is a low-level method, consider using extract_thumb() instead.