File: color.py

package info (click to toggle)
python-imgviz 1.7.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,056 kB
  • sloc: python: 3,131; makefile: 25
file content (238 lines) | stat: -rw-r--r-- 5,328 bytes parent folder | download | duplicates (2)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import numpy as np

from . import dtype
from . import utils


def rgb2gray(rgb):
    # type: (np.ndarray) -> np.ndarray
    """Covnert rgb to gray.

    Parameters
    ----------
    rgb: numpy.ndarray, (H, W, 3), np.uint8
        Input rgb image.

    Returns
    -------
    gray: numpy.ndarray, (H, W)
        Output gray image.

    """
    assert rgb.ndim == 3, "rgb must be 3 dimensional"
    assert rgb.shape[2] == 3, "rgb shape must be (H, W, 3)"
    assert rgb.dtype == np.uint8, "rgb dtype must be np.uint8"

    gray = utils.numpy_to_pillow(rgb)
    gray = gray.convert("L")
    gray = utils.pillow_to_numpy(gray)
    return gray


def gray2rgb(gray):
    # type: (np.ndarray) -> np.ndarray
    """Covnert gray to rgb.

    Parameters
    ----------
    gray: numpy.ndarray, (H, W), np.uint8
        Input gray image.

    Returns
    -------
    rgb: numpy.ndarray, (H, W, 3), np.uint8
        Output rgb image.

    """
    assert gray.ndim == 2, "gray must be 2 dimensional"
    assert gray.dtype == np.uint8, "gray dtype must be np.uint8"

    rgb = gray[:, :, None].repeat(3, axis=2)
    return rgb


def rgb2rgba(rgb):
    # type: (np.ndarray) -> np.ndarray
    """Convert rgb to rgba.

    Parameters
    ----------
    rgb: numpy.ndarray, (H, W, 3), np.uint8
        Input rgb image.

    Returns
    -------
    rgba: numpy.ndarray, (H, W, 4), np.uint8
        Output rgba image.

    """
    assert rgb.ndim == 3, "rgb must be 3 dimensional"
    assert rgb.shape[2] == 3, "rgb shape must be (H, W, 3)"
    assert rgb.dtype == np.uint8, "rgb dtype must be np.uint8"

    a = np.full(rgb.shape[:2], 255, dtype=np.uint8)
    rgba = np.dstack((rgb, a))
    return rgba


def rgb2hsv(rgb):
    # type: (np.ndarray) -> np.ndarray
    """Convert rgb to hsv.

    Parameters
    ----------
    rgb: numpy.ndarray, (H, W, 3), np.uint8
        Input rgb image.

    Returns
    -------
    hsv: numpy.ndarray, (H, W, 3), np.uint8
        Output hsv image.

    """
    hsv = utils.numpy_to_pillow(rgb, mode="RGB")
    hsv = hsv.convert("HSV")
    hsv = utils.pillow_to_numpy(hsv)
    return hsv


def hsv2rgb(hsv):
    # type: (np.ndarray) -> np.ndarray
    """Convert hsv to rgb.

    Parameters
    ----------
    hsv: numpy.ndarray, (H, W, 3), np.uint8
        Input hsv image.

    Returns
    -------
    rgb: numpy.ndarray, (H, W, 3), np.uint8
        Output rgb image.

    """
    rgb = utils.numpy_to_pillow(hsv, mode="HSV")
    rgb = rgb.convert("RGB")
    rgb = utils.pillow_to_numpy(rgb)
    return rgb


def rgba2rgb(rgba):
    # type: (np.ndarray) -> np.ndarray
    """Convert rgba to rgb.

    Parameters
    ----------
    rgba: numpy.ndarray, (H, W, 4), np.uint8
        Input rgba image.

    Returns
    -------
    rgb: numpy.ndarray, (H, W, 3), np.uint8
        Output rgb image.
    """
    rgb = rgba[:, :, :3]
    return rgb


def asgray(img):
    # type: (np.ndarray) -> np.ndarray
    """Convert any array to gray image.

    Parameters
    ----------
    img: numpy.ndarray
        Input image.

    Returns
    -------
    gray: numpy.ndarray, (H, W), np.uint8
        Output gray image.
    """
    if img.ndim == 2:
        if img.dtype == bool:
            gray = dtype.bool2ubyte(img)
        else:
            gray = img
    elif img.ndim == 3 and img.shape[2] == 4:
        gray = rgb2gray(rgba2rgb(img))
    elif img.ndim == 3 and img.shape[2] == 3:
        gray = rgb2gray(img)
    else:
        raise ValueError(
            "Unsupported image format to convert to gray:" "shape={}, dtype={}".format(
                img.shape, img.dtype
            )
        )
    return gray


def asrgb(img):
    # type: (np.ndarray) -> np.ndarray
    """Convert any array to rgb image.

    Parameters
    ----------
    img: numpy.ndarray
        Input image.

    Returns
    -------
    rgb: numpy.ndarray, (H, W, 3), np.uint8
        Output gray image.
    """
    if img.ndim == 2:
        if img.dtype == bool:
            img = dtype.bool2ubyte(img)
        rgb = gray2rgb(img)
    elif img.ndim == 3 and img.shape[2] == 4:
        rgb = rgba2rgb(img)
    elif img.ndim == 3 and img.shape[2] == 3:
        rgb = img
    else:
        raise ValueError(
            "Unsupported image format to convert to rgb:" "shape={}, dtype={}".format(
                img.shape, img.dtype
            )
        )
    return rgb


def asrgba(img):
    # type: (np.ndarray) -> np.ndarray
    """Convert any array to rgba image.

    Parameters
    ----------
    img: numpy.ndarray
        Input image.

    Returns
    -------
    rgba: numpy.ndarray, (H, W, 3), np.uint8
        Output gray image.
    """
    if img.ndim == 2:
        if img.dtype == bool:
            img = dtype.bool2ubyte(img)
        rgb = gray2rgb(img)
        rgba = rgb2rgba(rgb)
    if img.ndim == 3 and img.shape[2] == 4:
        rgba = img
    elif img.ndim == 3 and img.shape[2] == 3:
        rgba = rgb2rgba(img)
    else:
        raise ValueError(
            "Unsupported image format to convert to rgba:" "shape={}, dtype={}".format(
                img.shape, img.dtype
            )
        )
    return rgba


def get_fg_color(color):
    color = np.asarray(color, dtype=np.uint8)
    intensity = rgb2gray(color.reshape(1, 1, 3)).sum()
    if intensity > 170:
        return (0, 0, 0)
    return (255, 255, 255)