File size: 4,521 Bytes
fc262e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from pathlib import Path
from typing import Any, Union

import numpy as np

import cv2

from PIL import Image, ImageEnhance


def load_image(file_name, path_to_images=None, rgb: bool = True):
    path = (
        file_name
        if isinstance(file_name, Path) is True
        else path_to_images.joinpath(file_name)
    )

    try:
        img = cv2.imread(str(path))
        if rgb is True:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    except Exception as e:
        print(file_name)
    return img


def to_pil(image):
    return Image.fromarray(image)


def to_cv2(image):
    return np.array(image)


def enhance_pil_image(
    image, color=1, brightness=1, contrast=1, sharpness=1
) -> Image.Image:
    image = ImageEnhance.Sharpness(
        image=ImageEnhance.Brightness(
            image=ImageEnhance.Contrast(
                image=ImageEnhance.Color(
                    image=(
                        image
                        if isinstance(image, Image.Image) is True
                        else to_pil(image=image)
                    )
                ).enhance(color)
            ).enhance(contrast)
        ).enhance(brightness)
    ).enhance(sharpness)
    return image


def ensure_odd(
    i: int,
    min_val: Union[None, int] = None,
    max_val: Union[None, int] = None,
) -> int:
    """Transforms an odd number into pair number by adding one
    Arguments:
        i {int} -- number
    Returns:
        int -- Odd number
    """
    if (i > 0) and (i % 2 == 0):
        i += 1
    if min_val is not None:
        return max(i, min_val)
    if max_val is not None:
        return min(i, max_val)
    return i


def get_morphology_kernel(size: int, shape: int):
    """Builds morphology kernel
    :param size: kernel size, must be odd number
    :param shape: select shape of kernel
    :return: Morphology kernel
    """
    size = ensure_odd(size)
    return cv2.getStructuringElement(shape, (size, size))


def close(
    image: Any,
    kernel_size: int = 3,
    kernel_shape: int = cv2.MORPH_ELLIPSE,
    rois: tuple = (),
    proc_times: int = 1,
):
    """Morphology - Close wrapper
    Arguments:
        image {numpy array} -- Source image
        kernel_size {int} -- kernel size
        kernel_shape {int} -- cv2 constant
        roi -- Region of Interest
        proc_times {int} -- iterations
    Returns:
        numpy array -- closed image
    """
    morph_kernel = get_morphology_kernel(kernel_size, kernel_shape)
    if rois:
        result = image.copy()
        for roi in rois:
            r = roi.as_rect()
            result[r.top : r.bottom, r.left : r.right] = cv2.morphologyEx(
                result[r.top : r.bottom, r.left : r.right],
                cv2.MORPH_CLOSE,
                morph_kernel,
                iterations=proc_times,
            )
    else:
        result = cv2.morphologyEx(
            image, cv2.MORPH_CLOSE, morph_kernel, iterations=proc_times
        )
    return result


def get_concat_h_multi_resize(im_list, resample=Image.Resampling.BICUBIC):
    min_height = min(im.height for im in im_list)
    im_list_resize = [
        im.resize(
            (int(im.width * min_height / im.height), min_height), resample=resample
        )
        for im in im_list
    ]
    total_width = sum(im.width for im in im_list_resize)
    dst = Image.new("RGB", (total_width, min_height))
    pos_x = 0
    for im in im_list_resize:
        dst.paste(im, (pos_x, 0))
        pos_x += im.width
    return dst


def get_concat_v_multi_resize(im_list, resample=Image.Resampling.BICUBIC):
    min_width = min(im.width for im in im_list)
    im_list_resize = [
        im.resize((min_width, int(im.height * min_width / im.width)), resample=resample)
        for im in im_list
    ]
    total_height = sum(im.height for im in im_list_resize)
    dst = Image.new("RGB", (min_width, total_height))
    pos_y = 0
    for im in im_list_resize:
        dst.paste(im, (0, pos_y))
        pos_y += im.height
    return dst


def get_concat_tile_resize(im_list_2d, resample=Image.Resampling.BICUBIC):
    im_list_v = [
        get_concat_h_multi_resize(im_list_h, resample=resample)
        for im_list_h in im_list_2d
    ]
    return get_concat_v_multi_resize(im_list_v, resample=resample)


def get_tiles(img_list, row_count, resample=Image.Resampling.BICUBIC):
    if isinstance(img_list, np.ndarray) is False:
        img_list = np.asarray(img_list, dtype="object")
    return get_concat_tile_resize(np.split(img_list, row_count), resample)