aboutsummaryrefslogtreecommitdiffstats
path: root/bsie/reader/preview/_pillow.py
blob: 2b797c683124026180fbcb49aca0fe0dc29aea26 (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

# standard imports
from functools import partial
import typing

# external imports
import PIL.Image

# bsie imports
from bsie.utils import errors

# inner-module imports
from . import utils
from .. import base

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


## code ##

class PillowPreviewReader(base.Reader):
    """Produce previews for image files using the Pillow library."""

    def __call__(self, path: str) -> typing.Callable[[int], PIL.Image.Image]:
        try:
            # open file with PIL
            img = PIL.Image.open(path)
            # return callback
            return partial(utils.resize, img)
        except PIL.UnidentifiedImageError as err:
            # failed to open, skip file
            raise errors.UnsupportedFileFormatError(path) from err
        except OSError as err:
            raise errors.ReaderError(path) from err

# EOF ##