title: Use PIL to create an array of images with borders bteween images
body: <p>From <a href="https://stackoverflow.com/questions/35438802/making-a-collage-in-pil">this question</a> I have the following code:</p>
  <pre class="lang-py prettyprint-override"><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(&quot;out.png&quot;, &quot;PNG&quot;, quality=80, optimize=True, progressive=True)
  </code></pre>
  <p>But I need borders between images.  I then found <a href="https://stackoverflow.com/a/54066929/12069968">this answer here</a>, so I adapted the code as follows:</p>
  <pre class="lang-py prettyprint-override"><code>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(&quot;out.png&quot;, &quot;PNG&quot;, quality = 80, optimize = True, progressive = True)
  </code></pre>
  <p>Which is <em>almost</em> working, but now I am getting a border at the bottom of the image, which is not what I want.</p>
  <p>How do I get consistent horizontal and vertical borders between images of a collage, without them appearing outside the collage?</p>