From this question I have the following code:
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)
But I need borders between images. I then found this answer here, so I adapted the code as follows:
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)
Which is almost working, but now I am getting a border at the bottom of the image, which is not what I want.
How do I get consistent horizontal and vertical borders between images of a collage, without them appearing outside the collage?