RAW Image processing in Python

59.8k views Asked by At

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.

6

There are 6 answers

1
MHarrison On

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.

0
Unapiedra On

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.

3
Thomas Cassou On

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.

1
letmaik On

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.

0
Lester Cheung On

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. ;-)

0
vartec On

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.