| 1 |
import numpy as np |
| 2 |
|
| 3 |
from . import dtype |
| 4 |
from . import utils |
| 5 |
|
| 6 |
|
| 7 |
def rgb2gray(rgb): |
| 8 |
|
| 9 |
"""Covnert rgb to gray. |
| 10 |
|
| 11 |
Parameters |
| 12 |
---------- |
| 13 |
rgb: numpy.ndarray, (H, W, 3), np.uint8 |
| 14 |
Input rgb image. |
| 15 |
|
| 16 |
Returns |
| 17 |
------- |
| 18 |
gray: numpy.ndarray, (H, W) |
| 19 |
Output gray image. |
| 20 |
|
| 21 |
""" |
| 22 |
assert rgb.ndim == 3, "rgb must be 3 dimensional" |
| 23 |
assert rgb.shape[2] == 3, "rgb shape must be (H, W, 3)" |
| 24 |
assert rgb.dtype == np.uint8, "rgb dtype must be np.uint8" |
| 25 |
|
| 26 |
gray = utils.numpy_to_pillow(rgb) |
| 27 |
gray = gray.convert("L") |
| 28 |
gray = utils.pillow_to_numpy(gray) |
| 29 |
return gray |
| 30 |
|
| 31 |
|
| 32 |
def gray2rgb(gray): |
| 33 |
|
| 34 |
"""Covnert gray to rgb. |
| 35 |
|
| 36 |
Parameters |
| 37 |
---------- |
| 38 |
gray: numpy.ndarray, (H, W), np.uint8 |
| 39 |
Input gray image. |
| 40 |
|
| 41 |
Returns |
| 42 |
------- |
| 43 |
rgb: numpy.ndarray, (H, W, 3), np.uint8 |
| 44 |
Output rgb image. |
| 45 |
|
| 46 |
""" |
| 47 |
assert gray.ndim == 2, "gray must be 2 dimensional" |
| 48 |
assert gray.dtype == np.uint8, "gray dtype must be np.uint8" |
| 49 |
|
| 50 |
rgb = gray[:, :, None].repeat(3, axis=2) |
| 51 |
return rgb |
| 52 |
|
| 53 |
|
| 54 |
def rgb2rgba(rgb): |
| 55 |
|
| 56 |
"""Convert rgb to rgba. |
| 57 |
|
| 58 |
Parameters |
| 59 |
---------- |
| 60 |
rgb: numpy.ndarray, (H, W, 3), np.uint8 |
| 61 |
Input rgb image. |
| 62 |
|
| 63 |
Returns |
| 64 |
------- |
| 65 |
rgba: numpy.ndarray, (H, W, 4), np.uint8 |
| 66 |
Output rgba image. |
| 67 |
|
| 68 |
""" |
| 69 |
assert rgb.ndim == 3, "rgb must be 3 dimensional" |
| 70 |
assert rgb.shape[2] == 3, "rgb shape must be (H, W, 3)" |
| 71 |
assert rgb.dtype == np.uint8, "rgb dtype must be np.uint8" |
| 72 |
|
| 73 |
a = np.full(rgb.shape[:2], 255, dtype=np.uint8) |
| 74 |
rgba = np.dstack((rgb, a)) |
| 75 |
return rgba |
| 76 |
|
| 77 |
|
| 78 |
def rgb2hsv(rgb): |
| 79 |
|
| 80 |
"""Convert rgb to hsv. |
| 81 |
|
| 82 |
Parameters |
| 83 |
---------- |
| 84 |
rgb: numpy.ndarray, (H, W, 3), np.uint8 |
| 85 |
Input rgb image. |
| 86 |
|
| 87 |
Returns |
| 88 |
------- |
| 89 |
hsv: numpy.ndarray, (H, W, 3), np.uint8 |
| 90 |
Output hsv image. |
| 91 |
|
| 92 |
""" |
| 93 |
hsv = utils.numpy_to_pillow(rgb, mode="RGB") |
| 94 |
hsv = hsv.convert("HSV") |
| 95 |
hsv = utils.pillow_to_numpy(hsv) |
| 96 |
return hsv |
| 97 |
|
| 98 |
|
| 99 |
def hsv2rgb(hsv): |
| 100 |
|
| 101 |
"""Convert hsv to rgb. |
| 102 |
|
| 103 |
Parameters |
| 104 |
---------- |
| 105 |
hsv: numpy.ndarray, (H, W, 3), np.uint8 |
| 106 |
Input hsv image. |
| 107 |
|
| 108 |
Returns |
| 109 |
------- |
| 110 |
rgb: numpy.ndarray, (H, W, 3), np.uint8 |
| 111 |
Output rgb image. |
| 112 |
|
| 113 |
""" |
| 114 |
rgb = utils.numpy_to_pillow(hsv, mode="HSV") |
| 115 |
rgb = rgb.convert("RGB") |
| 116 |
rgb = utils.pillow_to_numpy(rgb) |
| 117 |
return rgb |
| 118 |
|
| 119 |
|
| 120 |
def rgba2rgb(rgba): |
| 121 |
|
| 122 |
"""Convert rgba to rgb. |
| 123 |
|
| 124 |
Parameters |
| 125 |
---------- |
| 126 |
rgba: numpy.ndarray, (H, W, 4), np.uint8 |
| 127 |
Input rgba image. |
| 128 |
|
| 129 |
Returns |
| 130 |
------- |
| 131 |
rgb: numpy.ndarray, (H, W, 3), np.uint8 |
| 132 |
Output rgb image. |
| 133 |
""" |
| 134 |
rgb = rgba[:, :, :3] |
| 135 |
return rgb |
| 136 |
|
| 137 |
|
| 138 |
def asgray(img): |
| 139 |
|
| 140 |
"""Convert any array to gray image. |
| 141 |
|
| 142 |
Parameters |
| 143 |
---------- |
| 144 |
img: numpy.ndarray |
| 145 |
Input image. |
| 146 |
|
| 147 |
Returns |
| 148 |
------- |
| 149 |
gray: numpy.ndarray, (H, W), np.uint8 |
| 150 |
Output gray image. |
| 151 |
""" |
| 152 |
if img.ndim == 2: |
| 153 |
if img.dtype == bool: |
| 154 |
gray = dtype.bool2ubyte(img) |
| 155 |
else: |
| 156 |
gray = img |
| 157 |
elif img.ndim == 3 and img.shape[2] == 4: |
| 158 |
gray = rgb2gray(rgba2rgb(img)) |
| 159 |
elif img.ndim == 3 and img.shape[2] == 3: |
| 160 |
gray = rgb2gray(img) |
| 161 |
else: |
| 162 |
raise ValueError( |
| 163 |
"Unsupported image format to convert to gray:" "shape={}, dtype={}".format( |
| 164 |
img.shape, img.dtype |
| 165 |
) |
| 166 |
) |
| 167 |
return gray |
| 168 |
|
| 169 |
|
| 170 |
def asrgb(img): |
| 171 |
|
| 172 |
"""Convert any array to rgb image. |
| 173 |
|
| 174 |
Parameters |
| 175 |
---------- |
| 176 |
img: numpy.ndarray |
| 177 |
Input image. |
| 178 |
|
| 179 |
Returns |
| 180 |
------- |
| 181 |
rgb: numpy.ndarray, (H, W, 3), np.uint8 |
| 182 |
Output gray image. |
| 183 |
""" |
| 184 |
if img.ndim == 2: |
| 185 |
if img.dtype == bool: |
| 186 |
img = dtype.bool2ubyte(img) |
| 187 |
rgb = gray2rgb(img) |
| 188 |
elif img.ndim == 3 and img.shape[2] == 4: |
| 189 |
rgb = rgba2rgb(img) |
| 190 |
elif img.ndim == 3 and img.shape[2] == 3: |
| 191 |
rgb = img |
| 192 |
else: |
| 193 |
raise ValueError( |
| 194 |
"Unsupported image format to convert to rgb:" "shape={}, dtype={}".format( |
| 195 |
img.shape, img.dtype |
| 196 |
) |
| 197 |
) |
| 198 |
return rgb |
| 199 |
|
| 200 |
|
| 201 |
def asrgba(img): |
| 202 |
|
| 203 |
"""Convert any array to rgba image. |
| 204 |
|
| 205 |
Parameters |
| 206 |
---------- |
| 207 |
img: numpy.ndarray |
| 208 |
Input image. |
| 209 |
|
| 210 |
Returns |
| 211 |
------- |
| 212 |
rgba: numpy.ndarray, (H, W, 3), np.uint8 |
| 213 |
Output gray image. |
| 214 |
""" |
| 215 |
if img.ndim == 2: |
| 216 |
if img.dtype == bool: |
| 217 |
img = dtype.bool2ubyte(img) |
| 218 |
rgb = gray2rgb(img) |
| 219 |
rgba = rgb2rgba(rgb) |
| 220 |
if img.ndim == 3 and img.shape[2] == 4: |
| 221 |
rgba = img |
| 222 |
elif img.ndim == 3 and img.shape[2] == 3: |
| 223 |
rgba = rgb2rgba(img) |
| 224 |
else: |
| 225 |
raise ValueError( |
| 226 |
"Unsupported image format to convert to rgba:" "shape={}, dtype={}".format( |
| 227 |
img.shape, img.dtype |
| 228 |
) |
| 229 |
) |
| 230 |
return rgba |
| 231 |
|
| 232 |
|
| 233 |
def get_fg_color(color): |
| 234 |
color = np.asarray(color, dtype=np.uint8) |
| 235 |
intensity = rgb2gray(color.reshape(1, 1, 3)).sum() |
| 236 |
if intensity > 170: |
| 237 |
return (0, 0, 0) |
| 238 |
return (255, 255, 255) |