blob: 257fdb39ef164b299e3e66fe2dc1b558f5fc52d7 (
plain)
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
|
"""
Part of the bsie module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# standard imports
import typing
# external imports
import PIL.Image
import rawpy
# bsie imports
from bsie.utils import errors, filematcher
# inner-module imports
from .. import base
# constants
MATCH_RULE = 'mime={image/x-nikon-nef} | extension={nef}'
# exports
__all__: typing.Sequence[str] = (
'RawImage',
)
## code ##
class RawImage(base.Reader):
"""Use rawpy to read content of raw image file types."""
# file matcher
_match: filematcher.Matcher
# additional kwargs to rawpy's postprocess
_rawpy_kwargs: typing.Dict[str, typing.Any]
def __init__(self, **rawpy_kwargs):
match_rule = rawpy_kwargs.pop('file_match_rule', MATCH_RULE)
self._match = filematcher.parse(match_rule)
self._rawpy_kwargs = rawpy_kwargs
def __call__(self, path: str) -> PIL.Image.Image:
# perform quick checks first
if not self._match(path):
raise errors.UnsupportedFileFormatError(path)
try:
# open file with rawpy
ary = rawpy.imread(path).postprocess(**self._rawpy_kwargs)
# convert to PIL.Image
return PIL.Image.fromarray(ary)
except (rawpy.LibRawFatalError, # pylint: disable=no-member # pylint doesn't find the errors
rawpy.NotSupportedError, # pylint: disable=no-member
rawpy.LibRawNonFatalError, # pylint: disable=no-member
) as err:
raise errors.ReaderError(path) from err
## EOF ##
|