Created
February 2, 2021 17:01
-
-
Save celoyd/abcfa0881d25132cfa08a3c08b61971e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Demo a brightened luminance-only inversion on 8-bit images | |
from skimage import io | |
import numpy as np | |
from sys import argv | |
# RGB <-> YCoCg-R almost straight from Wikipedia | |
def rgb_to_yog(rgb: np.array) -> np.array: | |
R, G, B = rgb[..., 0], rgb[..., 1], rgb[..., 2] | |
Co = R - B | |
tmp = B + Co / 2 | |
Cg = G - tmp | |
Y = tmp + Cg / 2 | |
return np.dstack([Y, Co, Cg]) | |
def yog_to_rgb(yog: np.array) -> np.array: | |
Y, Co, Cg = yog[..., 0], yog[..., 1], yog[..., 2] | |
tmp = Y - Cg / 2 | |
G = Cg + tmp | |
B = tmp - Co / 2 | |
R = B + Co | |
return np.dstack([R, G, B]) | |
def invert_y(yog: np.array) -> np.array: | |
yog[..., 0] *= -1 | |
yog[..., 0] += 1 | |
return yog | |
def apply_gamma(yog: np.array, gamma: float) -> np.array: | |
yog[..., 0] **= gamma | |
return yog | |
def read(path: str) -> np.array: | |
return io.imread(path).astype(np.float32) / 255 | |
def write(path: str, img: np.array): | |
img = np.clip(img * 255, 0, 255).astype(np.uint8) | |
io.imsave(path, img) | |
src_path, gamma, dst_path = argv[1:] | |
src = read(src_path) | |
gamma = float(gamma) | |
yog = rgb_to_yog(src) | |
yog = invert_y(yog) | |
yog = apply_gamma(yog, gamma) | |
rgb = yog_to_rgb(yog) | |
write(dst_path, rgb) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment