Spaces:
Runtime error
Runtime error
File size: 1,364 Bytes
c19ca42 |
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 |
import numpy as np
from ...utils.utils import get_h_w_c
from ..image_op import ImageOp
def passthrough_single_color(img: np.ndarray, scale: int, op: ImageOp) -> np.ndarray:
"""
If the given image is a single-color image, it will be scaled and returned as is instead of being processed by the given operation.
Obviously, this optimization is only correct if `op` doesn't change the color of single-color images.
To make this a transparent optimization, it is important that `scale` is correct.
`scale` must be the same factor by which `op` changes the dimension of the image.
"""
h, w, c = get_h_w_c(img)
if c == 1:
unique_list = np.unique(img)
if len(unique_list) == 1:
return np.full((h * scale, w * scale), unique_list[0], np.float32)
else:
unique_values = []
is_unique = True
for channel in range(c):
unique_list = np.unique(img[:, :, channel])
if len(unique_list) == 1:
unique_values.append(unique_list[0])
else:
is_unique = False
break
if is_unique:
channels = [
np.full((h * scale, w * scale), unique_values[channel], np.float32)
for channel in range(c)
]
return np.dstack(channels)
return op(img)
|