I'm trying to make a 3x3 Grid by sequence images but can't seem to get it right. The images are in folder, named from 0 - 8 (total 9 images), the output of the final one image grid of 3x3 should as follow
image0 image1 image2
image3 image4 image5
image6 image7 image8
I was trying to follow How do you merge images into a canvas using PIL/Pillow? but couldn't get it work correctly.
There are no need to change anything in the image, just merge them and make a 3x3 Grid
To make a grid of arbitrary shape (cols*img_height, rows*img_width)
out of rows*cols
images:
def image_grid(imgs, rows, cols):
assert len(imgs) == rows*cols
w, h = imgs[0].size
grid = Image.new('RGB', size=(cols*w, rows*h))
grid_w, grid_h = grid.size
for i, img in enumerate(imgs):
grid.paste(img, box=(i%cols*w, i//cols*h))
return grid
In your case, assuming imgs
is a list of PIL
images:
grid = image_grid(imgs, rows=3, cols=3)
Here's an example how this can be done (consider image is one of your images):
img_w, img_h = image.size
background = Image.new('RGBA',(1300, 1300), (255, 255, 255, 255))
bg_w, bg_h = background.size
offset = (10,(((bg_h - img_h)) / 2)-370)
background.paste(image1,offset)
Adjust the offset, width and height to fit your requirements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With