Spaces:
Sleeping
Sleeping
| # "Production code" | |
| import numpy as np | |
| from interactive_pipe import Curve, SingleCurve | |
| # ----------------- | |
| # Processing blocks | |
| # ----------------- | |
| def gen_color( | |
| frequency: int = 0, # discrete slider (int) | |
| isotropy: float = 0., # continuous slider (float) | |
| ) -> np.ndarray: | |
| lin_coord = np.linspace(0, 1., 256) | |
| X, Y = np.meshgrid(lin_coord, isotropy*lin_coord) | |
| radius = 0.5+0.5*np.cos(frequency*np.sqrt(X**2 + Y**2)) | |
| return np.stack([np.abs(X), np.abs(Y), radius], axis=-1).clip(0, 1) | |
| def modify_geometry( | |
| img: np.ndarray, | |
| effect: str = "flip" # dropdown menus (str) | |
| ) -> np.ndarray: | |
| img = img[::-1] if "flip" in effect else img | |
| img = img[:, ::-1] if "mirror" in effect else img | |
| return img | |
| def change_color( | |
| img: np.ndarray, | |
| bnw: bool = True # checkboxes (bool) | |
| ): | |
| if bnw: | |
| return np.mean(img, axis=-1, keepdims=True).repeat(3, axis=-1) | |
| return img | |
| def compare_by_splitting( | |
| img_1: np.ndarray, | |
| img_2: np.ndarray, | |
| ratio: float = 0.5, # continuous slider (float) | |
| ) -> np.ndarray: | |
| out = np.zeros_like(img_1) | |
| split = int(ratio*img_1.shape[1]) | |
| out[:, :split] = img_2[:, :split] | |
| out[:, split+5:] = img_1[:, split+5:] | |
| return out | |
| def extract_profile(img: np.ndarray,) -> Curve: | |
| luma = img.mean(axis=-1) | |
| h_profile = SingleCurve(y=luma[0, :], label="H profile") | |
| v_profile = SingleCurve(y=luma[:, 0], label="V profile") | |
| return Curve( | |
| [h_profile, v_profile], | |
| xlabel="Position", ylabel="Luminance", | |
| grid=True | |
| ) | |
| # ------------------- | |
| # Pipeline definition | |
| # ------------------- | |
| def tutorial_pipeline(): | |
| inp = gen_color() | |
| out_geometry = modify_geometry(inp) | |
| out_bnw = change_color(inp) | |
| out_image = compare_by_splitting(out_geometry, out_bnw) | |
| out_profile = extract_profile(out_image) | |
| return [[inp, out_geometry], [out_profile, out_image]] | |