This repository has been archived by the owner on Oct 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
raw.py
65 lines (48 loc) · 1.89 KB
/
raw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
""":mod:`rawkit.raw` --- High-level raw file API
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
from rawkit.libraw import libraw
class Raw(object):
"""
Represents a raw file (of any format) and exposes development options to
the user.
For example, the basic workflow (open a file, process the file, save the
file) looks like this::
from rawkit.raw import Raw
with Raw(filename='some/raw/image.CR2') as raw:
raw.process()
raw.save(filename='some/destination/image.ppm')
:param filename: the name of a raw file to load
:type filename: :class:`basestring`
:returns: A raw object
:rtype: :class:`Raw`
"""
def __init__(self, filename=None):
"""Initializes a new Raw object."""
self.data = libraw.libraw_init(0)
libraw.libraw_open_file(self.data, bytes(filename, 'utf-8'))
def __enter__(self):
"""Return a Raw object for use in context managers."""
return self
def __exit__(self, exc_type, exc_value, traceback):
"""Clean up after ourselves when leaving the context manager."""
self.close()
def close(self):
"""Free the underlying raw representation."""
libraw.libraw_close(self.data)
def process(self, options=None):
"""
Unpack and process the raw data into something more usable.
"""
libraw.libraw_unpack(self.data)
libraw.libraw_dcraw_process(self.data)
def save(self, filename=None):
"""
Save the raw data as a new PPM or TIFF image. This method is provided
for convenience, but in general export should actually be handled by
another library (such as Wand).
Keyword arguments:
filename -- A filename (ending in ``.ppm`` or ``.tiff``) to save.
"""
libraw.libraw_dcraw_ppm_tiff_writer(
self.data, bytes(filename, 'utf-8'))