Spaces:
Paused
Paused
Download utils/resize.py from NguyenDinhHieu/EquiFashion: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/spaces/NguyenDinhHieu/EquiFashion/resolve/c9aa3dbc14d03d7433dc65376c547e34a8ec8ab9/utils/resize.py
- Command line
-
hf download hf://spaces/NguyenDinhHieu/EquiFashion@c9aa3dbc14d03d7433dc65376c547e34a8ec8ab9/utils/resize.py
-
curl -L -o resize.py https://huggingface.co/spaces/NguyenDinhHieu/EquiFashion/resolve/c9aa3dbc14d03d7433dc65376c547e34a8ec8ab9/utils/resize.py
1.35 kB
| import numpy as np | |
| import cv2 | |
| from PIL import Image | |
| def HWC3(x): | |
| assert x.dtype == np.uint8 | |
| if x.ndim == 2: | |
| x = x[:, :, None] | |
| assert x.ndim == 3 | |
| H, W, C = x.shape | |
| assert C == 1 or C == 3 or C == 4 | |
| if C == 3: | |
| return x | |
| if C == 1: | |
| return np.concatenate([x, x, x], axis=2) | |
| if C == 4: | |
| color = x[:, :, 0:3].astype(np.float32) | |
| alpha = x[:, :, 3:4].astype(np.float32) / 255.0 # normalization | |
| y = color * alpha + 255.0 * (1.0 - alpha) | |
| y = y.clip(0, 255).astype(np.uint8) | |
| return y | |
| def resize_image(input_image, resolution): | |
| H, W, C = input_image.shape | |
| H = float(H) | |
| W = float(W) | |
| k = float(resolution) / min(H, W) | |
| H *= k | |
| W *= k | |
| H = int(np.round(H / 64.0)) * 64 | |
| W = int(np.round(W / 64.0)) * 64 | |
| img = cv2.resize(input_image, (W, H), interpolation=cv2.INTER_LANCZOS4 if k > 1 else cv2.INTER_AREA) | |
| return img | |
| # if __name__ == "__main__": | |
| # image_path = "/data/lh/docker/project/HieraFashDiff/3.jpeg" | |
| # input_image = Image.open(image_path).convert("RGB") | |
| # input_image = np.array(input_image) # Convert PIL Image to NumPy array | |
| # input_image = HWC3(input_image) | |
| # img = resize_image(input_image, 512) | |
| # H, W, C = img.shape | |
| # filename = "result.jpg" | |
| # Image.fromarray(img).save(filename) | |