可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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.
回答2:
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.
回答3:
I wrote a pure-python library recently called rawphoto for processing raw images in python. Currently it only supports Canon CR2 files (I'll be adding Nikon formats soon hopefully). It's also on PyPi. It may be helpful to you if you're accessing CR2 files.
Example of extracting the JPEG preview image:
from rawphoto.cr2 import Cr2 blob = Cr2(filename="somefile.CR2").get_quarter_size_rgb() # Do something with the JPEG here... eg. save it: with open("output.jpeg", 'wb' as f: f.write(blob)
or, using the generic Raw class which will wrap any type of raw file as I add more:
from rawphoto.raw import Raw # The API here will probably change before the 1.0 release. blob = Raw(filename="somefile.CR2").fhandle.get_quarter_size_rgb() # Do something with the JPEG here... eg. save it: with open("output.jpeg", 'wb' as f: f.write(blob)
EDIT: This has been replaced by rawkit, ctypes based libraw bindings. RawKit will work much better and you should use it instead (rawphoto is now going to be deprecated and just used as an educational tool).
回答4:
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.
回答5:
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. ;-)
回答6:
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.
回答7:
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.