51

Are there any Pythonic solutions to reading and processing RAW images. Even if it's simply accessing a raw photo file (eg. cr2 or dng) and then outputting it as a jpeg.

Ideally a dcraw bindings for python, but anything else that can accomplish the came would be sufficient as well.

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
The Unknown
  • 19,224
  • 29
  • 77
  • 93

6 Answers6

28

A while ago I wrote a libraw/dcraw wrapper called rawpy. It is quite easy to use:

import rawpy
import imageio

raw = rawpy.imread('image.nef')
rgb = raw.postprocess()
imageio.imsave('default.tiff', rgb)

It works natively with numpy arrays and supports a lot of options, including direct access to the unprocessed Bayer data.

letmaik
  • 3,348
  • 1
  • 36
  • 43
8

ImageMagick supports most RAW formats and provides Python bindings.

As for dcraw bindings for Python: dcraw is written in C, so you can access it through ctypes module.

vartec
  • 131,205
  • 36
  • 218
  • 244
6

Here is a way to convert a canon CR2 image to a friendly format with rawkit, that works with its current implementation:

import numpy as np

from PIL import Image
from rawkit.raw import Raw

filename = '/path/to/your/image.cr2'
raw_image = Raw(filename)
buffered_image = np.array(raw_image.to_buffer())
image = Image.frombytes('RGB', (raw_image.metadata.width, raw_image.metadata.height), buffered_image)
image.save('/path/to/your/new/image.png', format='png')

Using a numpy array is not very elegant here but at least it works, I could not figure how to use PIL constructors to achieve the same.

Thomas Cassou
  • 69
  • 1
  • 4
1

Try http://libopenraw.freedesktop.org/wiki/GettingTheCode

Git repo: git://anongit.freedesktop.org/git/libopenraw.git

There is a python directory in the source tree. ;-)

Lester Cheung
  • 1,964
  • 16
  • 17
0

I found this: https://gitorious.org/dcraw-thumbnailer/mainline/blobs/master/dcraw-thumbnailer

It calls dcraw as a process from python and converts it to a PIL object.

Unapiedra
  • 15,037
  • 12
  • 64
  • 93
0

I'm not sure how extensive the RAW support in Python Imaging Library (PIL http://www.pythonware.com/products/pil/) is, but you may want to check that out.

Otherwise, you could just call dcraw directly since it already solves this problem nicely.

MHarrison
  • 182
  • 6