【发布时间】:2021-05-02 22:18:14
【问题描述】:
来自this question我有以下代码:
def create_collage(cols, rows, width, height, listofimages):
thumbnail_width = width//cols
thumbnail_height = height//rows
size = thumbnail_width, thumbnail_height
new_im = Image.new('RGB', (width, height)) # 300 pt border
ims = []
for p in listofimages:
im = Image.open(p)
im.thumbnail(size)
ims.append(im)
i = 0
x = 0
y = 0
for col in range(cols):
for row in range(rows):
new_im.paste(ims[i], (x, y))
i += 1
y += thumbnail_height
x += thumbnail_width
y = 0
new_im.save("out.png", "PNG", quality=80, optimize=True, progressive=True)
但我需要图像之间的边框。然后我找到了this answer here,所以我将代码修改如下:
def create_collage(images_per_row, img_width, img_height, padding, frame_width, images):
sf = (frame_width - (images_per_row - 1) * padding) / (images_per_row * img_width) # scaling factor
scaled_img_width = ceil(img_width * sf)
scaled_img_height = ceil(img_height * sf) + padding # THIS NEEDED CHANGING as no horizontal borders were showing
number_of_rows = ceil(len(images) / images_per_row)
frame_height = ceil(sf * img_height * number_of_rows)
new_im = Image.new('RGB', (frame_width, frame_height))
i, j =0, 0
for num, im in enumerate(images):
if num % images_per_row == 0:
i = 0
im = Image.open(im)
im.thumbnail((scaled_img_width, scaled_img_height))
y_cord = (j // images_per_row) * scaled_img_height
new_im.paste(im, (i, y_cord))
i = (i + scaled_img_width) + padding
j += 1
new_im.save("out.png", "PNG", quality = 80, optimize = True, progressive = True)
这几乎工作了,但现在我在图像底部得到了一个边框,这不是我想要的。
如何在拼贴图像之间获得一致的水平和垂直边界,而不会使它们出现在拼贴之外?
【问题讨论】:
标签: python python-3.x python-imaging-library