Spaces:
Running
on
Zero
Running
on
Zero
Upload imgproc.py
Browse files- imgproc.py +80 -0
imgproc.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
|
7 |
+
def center_crop_arr(pil_image, image_size):
|
8 |
+
"""
|
9 |
+
Center cropping implementation from ADM.
|
10 |
+
https://github.com/openai/guided-diffusion/blob/8fb3ad9197f16bbc40620447b2742e13458d2831/guided_diffusion/image_datasets.py#L126
|
11 |
+
"""
|
12 |
+
while min(*pil_image.size) >= 2 * image_size:
|
13 |
+
pil_image = pil_image.resize(tuple(x // 2 for x in pil_image.size), resample=Image.BOX)
|
14 |
+
|
15 |
+
scale = image_size / min(*pil_image.size)
|
16 |
+
pil_image = pil_image.resize(tuple(round(x * scale) for x in pil_image.size), resample=Image.BICUBIC)
|
17 |
+
|
18 |
+
arr = np.array(pil_image)
|
19 |
+
crop_y = (arr.shape[0] - image_size) // 2
|
20 |
+
crop_x = (arr.shape[1] - image_size) // 2
|
21 |
+
return Image.fromarray(arr[crop_y : crop_y + image_size, crop_x : crop_x + image_size])
|
22 |
+
|
23 |
+
|
24 |
+
def center_crop(pil_image, crop_size):
|
25 |
+
while pil_image.size[0] >= 2 * crop_size[0] and pil_image.size[1] >= 2 * crop_size[1]:
|
26 |
+
pil_image = pil_image.resize(tuple(x // 2 for x in pil_image.size), resample=Image.BOX)
|
27 |
+
|
28 |
+
scale = max(crop_size[0] / pil_image.size[0], crop_size[1] / pil_image.size[1])
|
29 |
+
pil_image = pil_image.resize(tuple(round(x * scale) for x in pil_image.size), resample=Image.BICUBIC)
|
30 |
+
|
31 |
+
# crop_left = random.randint(0, pil_image.size[0] - crop_size[0])
|
32 |
+
# crop_upper = random.randint(0, pil_image.size[1] - crop_size[1])
|
33 |
+
crop_left = (pil_image.size[0] - crop_size[0]) // 2
|
34 |
+
crop_upper = (pil_image.size[1] - crop_size[1]) // 2
|
35 |
+
crop_right = crop_left + crop_size[0]
|
36 |
+
crop_lower = crop_upper + crop_size[1]
|
37 |
+
return pil_image.crop(box=(crop_left, crop_upper, crop_right, crop_lower))
|
38 |
+
|
39 |
+
def var_center_crop(pil_image, crop_size_list, random_top_k=4):
|
40 |
+
w, h = pil_image.size
|
41 |
+
rem_percent = [min(cw / w, ch / h) / max(cw / w, ch / h) for cw, ch in crop_size_list]
|
42 |
+
crop_size = random.choice(
|
43 |
+
sorted(((x, y) for x, y in zip(rem_percent, crop_size_list)), reverse=True)[:random_top_k]
|
44 |
+
)[1]
|
45 |
+
return center_crop(pil_image, crop_size)
|
46 |
+
|
47 |
+
def var_center_crop_128(pil_image, crop_size_list, random_top_k=4):
|
48 |
+
w, h = pil_image.size
|
49 |
+
rem_percent = [min(cw / w, ch / h) / max(cw / w, ch / h) for cw, ch in crop_size_list]
|
50 |
+
crop_size = random.choice(
|
51 |
+
sorted(((x, y) for x, y in zip(rem_percent, crop_size_list)), reverse=True)[:random_top_k]
|
52 |
+
)[1]
|
53 |
+
breakpoint()
|
54 |
+
return center_crop(pil_image, (((w//128)*128), ((h//128)*128)))
|
55 |
+
|
56 |
+
|
57 |
+
def generate_crop_size_list(num_patches, patch_size, max_ratio=4.0):
|
58 |
+
assert max_ratio >= 1.0
|
59 |
+
crop_size_list = []
|
60 |
+
wp, hp = num_patches, 1
|
61 |
+
while wp > 0:
|
62 |
+
if max(wp, hp) / min(wp, hp) <= max_ratio:
|
63 |
+
if ((wp * patch_size)//32) % 2 == 0 and ((hp * patch_size)//32) % 2 == 0:
|
64 |
+
crop_size_list.append((wp * patch_size, hp * patch_size))
|
65 |
+
if (hp + 1) * wp <= num_patches:
|
66 |
+
hp += 1
|
67 |
+
else:
|
68 |
+
wp -= 1
|
69 |
+
return crop_size_list
|
70 |
+
|
71 |
+
|
72 |
+
def to_rgb_if_rgba(img: Image.Image):
|
73 |
+
if img.mode.upper() == "RGBA":
|
74 |
+
rgb_img = Image.new("RGB", img.size, (255, 255, 255))
|
75 |
+
rgb_img.paste(img, mask=img.split()[3]) # 3 is the alpha channel
|
76 |
+
return rgb_img
|
77 |
+
elif img.mode.upper() == "P":
|
78 |
+
return img.convert('RGB')
|
79 |
+
else:
|
80 |
+
return img
|