Spaces:
Runtime error
Runtime error
Update src/util.py
Browse files- src/util.py +19 -19
src/util.py
CHANGED
@@ -16,8 +16,8 @@ bucket_name = os.getenv("BUCKET_NAME")
|
|
16 |
endpoint = os.getenv("ENDPOINT")
|
17 |
|
18 |
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
|
19 |
-
oss_path = "
|
20 |
-
oss_path_img_gallery = "
|
21 |
|
22 |
|
23 |
def download_img_pil(index, img_url):
|
@@ -43,7 +43,7 @@ def download_images(img_urls, batch_size):
|
|
43 |
ret = future.result()
|
44 |
# worker_results.append(ret)
|
45 |
index, img_pil = ret
|
46 |
-
imgs_pil[index] = img_pil #
|
47 |
|
48 |
return imgs_pil
|
49 |
|
@@ -62,8 +62,8 @@ def upload_np_2_oss(input_image, name="cache.png", gallery=False):
|
|
62 |
else:
|
63 |
path = oss_path
|
64 |
|
65 |
-
bucket.put_object(path + "/" + name, imgByteArr) # data
|
66 |
-
ret = bucket.sign_url('GET', path + "/" + name, 60 * 60 * 24) #
|
67 |
del imgByteArr
|
68 |
return ret
|
69 |
|
@@ -74,8 +74,8 @@ def upload_json_string_2_oss(jsonStr, name="cache.txt", gallery=False):
|
|
74 |
else:
|
75 |
path = oss_path
|
76 |
|
77 |
-
bucket.put_object(path + "/" + name, bytes(jsonStr, "utf-8")) # data
|
78 |
-
ret = bucket.sign_url('GET', path + "/" + name, 60 * 60 * 24) #
|
79 |
return ret
|
80 |
|
81 |
|
@@ -128,15 +128,15 @@ def upload_preprocess(pil_base_image_rgba, pil_layout_image_dict, pil_style_imag
|
|
128 |
|
129 |
|
130 |
def pad_image(image, target_size):
|
131 |
-
iw, ih = image.size #
|
132 |
-
w, h = target_size #
|
133 |
-
scale = min(w / iw, h / ih) #
|
134 |
-
#
|
135 |
nw = int(iw * scale + 0.5)
|
136 |
nh = int(ih * scale + 0.5)
|
137 |
-
image = image.resize((nw, nh), Image.BICUBIC) #
|
138 |
-
new_image = Image.new('RGB', target_size, (255, 255, 255)) #
|
139 |
-
new_image.paste(image, ((w - nw) // 2, (h - nh) // 2)) #
|
140 |
return new_image
|
141 |
|
142 |
|
@@ -152,17 +152,17 @@ def add_text(image, text):
|
|
152 |
image = Image.blend(image, text_image, 0.5)
|
153 |
|
154 |
image_draw = ImageDraw.Draw(image)
|
155 |
-
fillColor = (0, 0, 0, 255) #
|
156 |
-
pos = (0, 0) #
|
157 |
image_draw.text(pos, text, font=ttf, fill=fillColor)
|
158 |
return image.convert("RGB")
|
159 |
|
160 |
|
161 |
def compose_image(image_list, text_list, pil_size, nrow, ncol):
|
162 |
-
w, h = pil_size #
|
163 |
|
164 |
if len(image_list) > nrow * ncol:
|
165 |
-
raise ValueError("
|
166 |
|
167 |
assert len(image_list) == len(text_list)
|
168 |
new_image_list = []
|
@@ -173,7 +173,7 @@ def compose_image(image_list, text_list, pil_size, nrow, ncol):
|
|
173 |
new_text_list.append(text)
|
174 |
if len(new_image_list) == 1:
|
175 |
ncol = nrow = 1
|
176 |
-
to_image = Image.new('RGB', (ncol * w, nrow * h), (255, 255, 255)) #
|
177 |
for y in range(1, nrow + 1):
|
178 |
for x in range(1, ncol + 1):
|
179 |
if ncol * (y - 1) + x - 1 < len(new_image_list):
|
|
|
16 |
endpoint = os.getenv("ENDPOINT")
|
17 |
|
18 |
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
|
19 |
+
oss_path = "nightfury.abc/ImageSynthesizerHF"
|
20 |
+
oss_path_img_gallery = "nightfury.abc/ImageSynthesizerHF_img_gallery"
|
21 |
|
22 |
|
23 |
def download_img_pil(index, img_url):
|
|
|
43 |
ret = future.result()
|
44 |
# worker_results.append(ret)
|
45 |
index, img_pil = ret
|
46 |
+
imgs_pil[index] = img_pil # Arrange the URLs in order, which will be used to download the associated images or svg later.
|
47 |
|
48 |
return imgs_pil
|
49 |
|
|
|
62 |
else:
|
63 |
path = oss_path
|
64 |
|
65 |
+
bucket.put_object(path + "/" + name, imgByteArr) # data is data, which can be a picture
|
66 |
+
ret = bucket.sign_url('GET', path + "/" + name, 60 * 60 * 24) # The return value is the link, and the parameters are, in order, method/file path on oss/expiration time (s)
|
67 |
del imgByteArr
|
68 |
return ret
|
69 |
|
|
|
74 |
else:
|
75 |
path = oss_path
|
76 |
|
77 |
+
bucket.put_object(path + "/" + name, bytes(jsonStr, "utf-8")) # data is data
|
78 |
+
ret = bucket.sign_url('GET', path + "/" + name, 60 * 60 * 24) # The return value is the link, and the parameters are, in order, method/file path on oss/expiration time (s)
|
79 |
return ret
|
80 |
|
81 |
|
|
|
128 |
|
129 |
|
130 |
def pad_image(image, target_size):
|
131 |
+
iw, ih = image.size # Original image size
|
132 |
+
w, h = target_size # The size of the target image
|
133 |
+
scale = min(w / iw, h / ih) # minimum ratio to convert
|
134 |
+
# Guaranteed length or width, at least one size that fits the target image 0.5 guarantees rounding
|
135 |
nw = int(iw * scale + 0.5)
|
136 |
nh = int(ih * scale + 0.5)
|
137 |
+
image = image.resize((nw, nh), Image.BICUBIC) # Changing the image size, bi-legislative interpolation works well
|
138 |
+
new_image = Image.new('RGB', target_size, (255, 255, 255)) # generate white image
|
139 |
+
new_image.paste(image, ((w - nw) // 2, (h - nh) // 2)) # Style that fills the image with the middle image and black on both sides
|
140 |
return new_image
|
141 |
|
142 |
|
|
|
152 |
image = Image.blend(image, text_image, 0.5)
|
153 |
|
154 |
image_draw = ImageDraw.Draw(image)
|
155 |
+
fillColor = (0, 0, 0, 255) # Text color: black
|
156 |
+
pos = (0, 0) # The position of the upper left corner of the text (distance from the left border, distance from the top border)
|
157 |
image_draw.text(pos, text, font=ttf, fill=fillColor)
|
158 |
return image.convert("RGB")
|
159 |
|
160 |
|
161 |
def compose_image(image_list, text_list, pil_size, nrow, ncol):
|
162 |
+
w, h = pil_size # Each small picture size
|
163 |
|
164 |
if len(image_list) > nrow * ncol:
|
165 |
+
raise ValueError("The parameters of the composite image and the required quantity do not match!")
|
166 |
|
167 |
assert len(image_list) == len(text_list)
|
168 |
new_image_list = []
|
|
|
173 |
new_text_list.append(text)
|
174 |
if len(new_image_list) == 1:
|
175 |
ncol = nrow = 1
|
176 |
+
to_image = Image.new('RGB', (ncol * w, nrow * h), (255, 255, 255)) # Create a new diagram
|
177 |
for y in range(1, nrow + 1):
|
178 |
for x in range(1, ncol + 1):
|
179 |
if ncol * (y - 1) + x - 1 < len(new_image_list):
|