aboutsummaryrefslogtreecommitdiffstats
path: root/bsie/reader/image/_pillow.py
blob: 5b2bdf2e3aff408bac64ee66ea5fb9afd70f14c2 (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
"""

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

# bsie imports
from bsie.utils import errors

# inner-module imports
from .. import base

# exports
__all__: typing.Sequence[str] = (
    'PillowImage',
    )


## code ##

class PillowImage(base.Reader):
    """Use PIL to read content of a variety of image file types."""

    def __call__(self, path: str) -> PIL.Image.Image:
        try:
            # open file with PIL
            return PIL.Image.open(path)
        except PIL.UnidentifiedImageError as err:
            raise errors.UnsupportedFileFormatError(path) from err
        except IOError as err:
            raise errors.ReaderError(path) from err

# EOF ##