How to merge images with same size using the python 3 module pillow ?

Published: February 01, 2019

DMCA.com Protection Status

To merge images, a solution is to use the python module called pillow. Let's take for example the following 4 images:


Step 1: open images using pillow

>>> from PIL import Image

>>> img_01 = Image.open("digit-number-img-0.JPEG")
>>> img_02 = Image.open("digit-number-img-1.JPEG")
>>> img_03 = Image.open("digit-number-img-2.JPEG")
>>> img_04 = Image.open("digit-number-img-3.JPEG")

Step 2: Get image size

>>> img_01_size = img_01.size
>>> img_02_size = img_02.size
>>> img_03_size = img_02.size
>>> img_02_size = img_02.size

>>> print('img 1 size: ', img_01_size)
>>> print('img 2 size: ', img_02_size)
>>> print('img 3 size: ', img_03_size)
>>> print('img 4 size: ', img_03_size)

Step 3: Create an empty white image:

>>> new_im = Image.new('RGB', (2*img_01_size[0],2*img_01_size[1]), (250,250,250))

Step 4: Paste images

>>> new_im.paste(img_01, (0,0))
>>> new_im.paste(img_02, (img_01_size[0],0))
>>> new_im.paste(img_03, (0,img_01_size[1]))
>>> new_im.paste(img_04, (img_01_size[0],img_01_size[1]))

Step 5: Save new image

>>> new_im.save("merged_images.png", "PNG")
>>> new_im.show()

Results:

Fusionner plusieurs images de même taille en utilisant pillow et python 3
Fusionner plusieurs images de même taille en utilisant pillow et python 3

References

Links Site
pillow pillow
how to merge images using python PIL library stackoverflow
Image

of