File size: 1,192 Bytes
f949b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
import PIL
from PIL import Image
import numpy as np
from dataclasses import dataclass

# TODO add register new converter so that it is accessible via converters.to_x

def ensure_class(func, params):
    def func_wrapper(function):
        def wrapper(self=None, *args, **kwargs):
            for key in kwargs:
                if key in params:
                    kwargs[key] = func(kwargs[key])
            if self is not None:
                return function(self, *args, **kwargs)
            else:
                return function(*args, **kwargs)

        return wrapper

    return func_wrapper


def as_PIL(img):
    if not isinstance(img, PIL.Image.Image):
        if isinstance(img, Path):
            img = img.as_posix()
        if isinstance(img, str):
            img = Image.open(img)
        elif isinstance(img, np.ndarray):
            img = Image.fromarray(img)

        else:
            raise NotImplementedError
    return img


def to_ndarray(input):
    if not isinstance(input, np.ndarray):
        input = np.array(input)
    return input


def to_Path(input):
    if not isinstance(input, Path):
        input = Path(input)
    return input