huzefa11 commited on
Commit
c60bb16
·
verified ·
1 Parent(s): 142dec5

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +2 -34
utils.py CHANGED
@@ -11,15 +11,7 @@ def get_random_bool():
11
  return random.choice([True, False])
12
 
13
  def add_white_border(input_image, border_width=10):
14
- """
15
- 为PIL图像添加指定宽度的白色边框。
16
-
17
- :param input_image: PIL图像对象
18
- :param border_width: 边框宽度(单位:像素)
19
- :return: 带有白色边框的PIL图像对象
20
- """
21
- border_color = 'white' # 白色边框
22
- # 添加边框
23
  img_with_border = ImageOps.expand(input_image, border=border_width, fill=border_color)
24
  return img_with_border
25
 
@@ -71,7 +63,7 @@ def add_caption(image, text, position = "bottom-mid", font = None, text_color=
71
  elif position == 'bottom-left':
72
  text_position = (10, height - (text_height + 20))
73
  elif position == 'bottom-mid':
74
- text_position = ((width - text_width) // 2, height - (text_height + 20) ) # 居中文本
75
  height = text_position[1]
76
  maxwidth = max(maxwidth,text_width)
77
  text_positions.append(text_position)
@@ -200,26 +192,20 @@ def get_row_image2(images,captions = None, font = None):
200
 
201
 
202
  def concat_images_vertically_and_scale(images,scale_factor=2):
203
- # 加载所有图像
204
- # 确保所有图像的宽度一致
205
  widths = [img.width for img in images]
206
  if not all(width == widths[0] for width in widths):
207
  raise ValueError('All images must have the same width.')
208
 
209
- # 计算总高度
210
  total_height = sum(img.height for img in images)
211
 
212
- # 创建新的图像,宽度与原图相同,高度为所有图像高度之和
213
  max_width = max(widths)
214
  concatenated_image = Image.new('RGB', (max_width, total_height))
215
 
216
- # 竖直拼接图像
217
  current_height = 0
218
  for img in images:
219
  concatenated_image.paste(img, (0, current_height))
220
  current_height += img.height
221
 
222
- # 缩放图像为1/n高度
223
  new_height = concatenated_image.height // scale_factor
224
  new_width = concatenated_image.width // scale_factor
225
  resized_image = concatenated_image.resize((new_width, new_height), Image.LANCZOS)
@@ -228,19 +214,13 @@ def concat_images_vertically_and_scale(images,scale_factor=2):
228
 
229
 
230
  def combine_images_horizontally(images):
231
- # 读取所有图片并存入列表
232
-
233
- # 获取每幅图像的宽度和高度
234
  widths, heights = zip(*(i.size for i in images))
235
 
236
- # 计算总宽度和最大高度
237
  total_width = sum(widths)
238
  max_height = max(heights)
239
 
240
- # 创建新的空白图片,用于拼接
241
  new_im = Image.new('RGB', (total_width, max_height))
242
 
243
- # 将图片横向拼接
244
  x_offset = 0
245
  for im in images:
246
  new_im.paste(im, (x_offset, 0))
@@ -250,28 +230,20 @@ def combine_images_horizontally(images):
250
 
251
  def combine_images_vertically_with_resize(images):
252
 
253
- # 获取所有图片的宽度和高度
254
  widths, heights = zip(*(i.size for i in images))
255
 
256
- # 确定新图片的宽度,即所有图片中最小的宽度
257
  min_width = min(widths)
258
 
259
- # 调整图片尺寸以保持宽度一致,长宽比不变
260
  resized_images = []
261
  for img in images:
262
- # 计算新高度保持图片长宽比
263
  new_height = int(min_width * img.height / img.width)
264
- # 调整图片大小
265
  resized_img = img.resize((min_width, new_height), Image.LANCZOS)
266
  resized_images.append(resized_img)
267
 
268
- # 计算所有调整尺寸后图片的总高度
269
  total_height = sum(img.height for img in resized_images)
270
 
271
- # 创建一个足够宽和高的新图片对象
272
  new_im = Image.new('RGB', (min_width, total_height))
273
 
274
- # 竖直拼接图片
275
  y_offset = 0
276
  for im in resized_images:
277
  new_im.paste(im, (0, y_offset))
@@ -310,10 +282,7 @@ def distribute_images(images, group_sizes=(4, 3, 2)):
310
  remaining = len(images)
311
 
312
  while remaining > 0:
313
- # 优先分配最大组(4张图片),再考虑3张,最后处理2张
314
  for size in sorted(group_sizes, reverse=True):
315
- # 如果剩下的图片数量大于等于当前组大小,或者为图片总数时(也就是第一次迭代)
316
- # 开始创建新组
317
  if remaining >= size or remaining == len(images):
318
  if remaining > size:
319
  new_group = images[-remaining: -remaining + size]
@@ -322,7 +291,6 @@ def distribute_images(images, group_sizes=(4, 3, 2)):
322
  groups.append(new_group)
323
  remaining -= size
324
  break
325
- # 如果剩下的图片少于最小的组大小(2张)并且已经有组了,就把剩下的图片加到最后一个组
326
  elif remaining < min(group_sizes) and groups:
327
  groups[-1].extend(images[-remaining:])
328
  remaining = 0
 
11
  return random.choice([True, False])
12
 
13
  def add_white_border(input_image, border_width=10):
14
+ border_color = 'white'
 
 
 
 
 
 
 
 
15
  img_with_border = ImageOps.expand(input_image, border=border_width, fill=border_color)
16
  return img_with_border
17
 
 
63
  elif position == 'bottom-left':
64
  text_position = (10, height - (text_height + 20))
65
  elif position == 'bottom-mid':
66
+ text_position = ((width - text_width) // 2, height - (text_height + 20) )
67
  height = text_position[1]
68
  maxwidth = max(maxwidth,text_width)
69
  text_positions.append(text_position)
 
192
 
193
 
194
  def concat_images_vertically_and_scale(images,scale_factor=2):
 
 
195
  widths = [img.width for img in images]
196
  if not all(width == widths[0] for width in widths):
197
  raise ValueError('All images must have the same width.')
198
 
 
199
  total_height = sum(img.height for img in images)
200
 
 
201
  max_width = max(widths)
202
  concatenated_image = Image.new('RGB', (max_width, total_height))
203
 
 
204
  current_height = 0
205
  for img in images:
206
  concatenated_image.paste(img, (0, current_height))
207
  current_height += img.height
208
 
 
209
  new_height = concatenated_image.height // scale_factor
210
  new_width = concatenated_image.width // scale_factor
211
  resized_image = concatenated_image.resize((new_width, new_height), Image.LANCZOS)
 
214
 
215
 
216
  def combine_images_horizontally(images):
 
 
 
217
  widths, heights = zip(*(i.size for i in images))
218
 
 
219
  total_width = sum(widths)
220
  max_height = max(heights)
221
 
 
222
  new_im = Image.new('RGB', (total_width, max_height))
223
 
 
224
  x_offset = 0
225
  for im in images:
226
  new_im.paste(im, (x_offset, 0))
 
230
 
231
  def combine_images_vertically_with_resize(images):
232
 
 
233
  widths, heights = zip(*(i.size for i in images))
234
 
 
235
  min_width = min(widths)
236
 
 
237
  resized_images = []
238
  for img in images:
 
239
  new_height = int(min_width * img.height / img.width)
 
240
  resized_img = img.resize((min_width, new_height), Image.LANCZOS)
241
  resized_images.append(resized_img)
242
 
 
243
  total_height = sum(img.height for img in resized_images)
244
 
 
245
  new_im = Image.new('RGB', (min_width, total_height))
246
 
 
247
  y_offset = 0
248
  for im in resized_images:
249
  new_im.paste(im, (0, y_offset))
 
282
  remaining = len(images)
283
 
284
  while remaining > 0:
 
285
  for size in sorted(group_sizes, reverse=True):
 
 
286
  if remaining >= size or remaining == len(images):
287
  if remaining > size:
288
  new_group = images[-remaining: -remaining + size]
 
291
  groups.append(new_group)
292
  remaining -= size
293
  break
 
294
  elif remaining < min(group_sizes) and groups:
295
  groups[-1].extend(images[-remaining:])
296
  remaining = 0