Upload extensions_built_in/diffusion_models/omnigen2/src/utils/img_util.py with huggingface_hub
Browse files
extensions_built_in/diffusion_models/omnigen2/src/utils/img_util.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List
|
| 2 |
+
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
from torchvision.transforms.functional import to_pil_image
|
| 7 |
+
|
| 8 |
+
def resize_image(image, max_pixels, img_scale_num):
|
| 9 |
+
width, height = image.size
|
| 10 |
+
cur_pixels = height * width
|
| 11 |
+
ratio = (max_pixels / cur_pixels) ** 0.5
|
| 12 |
+
ratio = min(ratio, 1.0) # do not upscale input image
|
| 13 |
+
|
| 14 |
+
new_height, new_width = int(height * ratio) // img_scale_num * img_scale_num, int(width * ratio) // img_scale_num * img_scale_num
|
| 15 |
+
|
| 16 |
+
image = image.resize((new_width, new_height), resample=Image.BICUBIC)
|
| 17 |
+
return image
|
| 18 |
+
|
| 19 |
+
def create_collage(images: List[torch.Tensor]) -> Image.Image:
|
| 20 |
+
"""Create a horizontal collage from a list of images."""
|
| 21 |
+
max_height = max(img.shape[-2] for img in images)
|
| 22 |
+
total_width = sum(img.shape[-1] for img in images)
|
| 23 |
+
canvas = torch.zeros((3, max_height, total_width), device=images[0].device)
|
| 24 |
+
|
| 25 |
+
current_x = 0
|
| 26 |
+
for img in images:
|
| 27 |
+
h, w = img.shape[-2:]
|
| 28 |
+
canvas[:, :h, current_x:current_x+w] = img * 0.5 + 0.5
|
| 29 |
+
current_x += w
|
| 30 |
+
|
| 31 |
+
return to_pil_image(canvas)
|