Spaces:
Sleeping
Sleeping
Download diffusion_core/utils/image_utils.py from hugging-apps/inverse-and-edit: direct link, hf CLI and curl.
- Browser
- Download file 855 Bytes
-
https://huggingface.co/spaces/hugging-apps/inverse-and-edit/resolve/main/diffusion_core/utils/image_utils.py
- Command line
-
hf download hf://spaces/hugging-apps/inverse-and-edit/diffusion_core/utils/image_utils.py
-
curl -L -o image_utils.py https://huggingface.co/spaces/hugging-apps/inverse-and-edit/resolve/main/diffusion_core/utils/image_utils.py
855 Bytes
| import numpy as np | |
| from PIL import Image | |
| def load_512(image_path, left=0, right=0, top=0, bottom=0): | |
| if type(image_path) is str: | |
| image = np.array(Image.open(image_path).convert('RGB'))[:, :, :3] | |
| else: | |
| image = image_path | |
| h, w, c = image.shape | |
| left = min(left, w-1) | |
| right = min(right, w - left - 1) | |
| top = min(top, h - left - 1) | |
| bottom = min(bottom, h - top - 1) | |
| image = image[top:h-bottom, left:w-right] | |
| h, w, c = image.shape | |
| if h < w: | |
| offset = (w - h) // 2 | |
| image = image[:, offset:offset + h] | |
| elif w < h: | |
| offset = (h - w) // 2 | |
| image = image[offset:offset + w] | |
| image = np.array(Image.fromarray(image).resize((512, 512))) | |
| return image | |
| def to_im(torch_image, **kwargs): | |
| return transforms.ToPILImage()( | |
| make_grid(torch_image, **kwargs) | |
| ) | |