nightfury commited on
Commit
eeef25d
1 Parent(s): 1ccfa62

Update src/util.py

Browse files
Files changed (1) hide show
  1. 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 = "hejunjie.hjj/TransferAnythingHF"
20
- oss_path_img_gallery = "hejunjie.hjj/TransferAnythingHF_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 # 按顺序排列url,后续下载关联的图片或者svg需要使用
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) # 返回值为链接,参数依次为,方法/oss上文件路径/过期时间(s)
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) # 返回值为链接,参数依次为,方法/oss上文件路径/过期时间(s)
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
- # 保证长或宽,至少一个符合目标图像的尺寸 0.5保证四舍五入
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):