Skip to content

Instantly share code, notes, and snippets.

@glombard
Created November 24, 2014 00:22
Show Gist options
  • Save glombard/7cd166e311992a828675 to your computer and use it in GitHub Desktop.
Save glombard/7cd166e311992a828675 to your computer and use it in GitHub Desktop.
Merging 4 images into one with Python and PIL/Pillow
# Combine multiple images into one.
#
# To install the Pillow module on Mac OS X:
#
# $ xcode-select --install
# $ brew install libtiff libjpeg webp little-cms2
# $ pip install Pillow
#
from __future__ import print_function
import os
from PIL import Image
files = [
'~/Downloads/1.jpg',
'~/Downloads/2.jpg',
'~/Downloads/3.jpg',
'~/Downloads/4.jpg']
result = Image.new("RGB", (800, 800))
for index, file in enumerate(files):
path = os.path.expanduser(file)
img = Image.open(path)
img.thumbnail((400, 400), Image.ANTIALIAS)
x = index // 2 * 400
y = index % 2 * 400
w, h = img.size
print('pos {0},{1} size {2},{3}'.format(x, y, w, h))
result.paste(img, (x, y, x + w, y + h))
result.save(os.path.expanduser('~/image.jpg'))
@Chrisbuuren
Copy link

You saved me , thanks a lot !

@Swikar
Copy link

Swikar commented May 4, 2016

Thanks a lot for sharing. My requirement is to combine 3 images to make book cover. That is Front Image, Spine, Back Image

@jay-pee
Copy link

jay-pee commented Jun 27, 2016

Hey :)
I forked your cool snipped. Check it out!

@lee1043
Copy link

lee1043 commented Aug 23, 2016

It is so much helpful, thank you for sharing!

@nramlia
Copy link

nramlia commented Jun 30, 2017

I need to read two images from cameras in raspberry pi and then save the images side by side

@gledsonmelotti
Copy link

gledsonmelotti commented Apr 10, 2018

Hi, how are you? I have a question about concatenating the layers of the images. For example. I have two images: one RGB (three layers) and another grayscale (one layer) of the same size. How do I join (unite) the two images, that is, join (unite) the four layers into a single array? I need is to have a 4-layer output.

@ChiragSoni95
Copy link

Hello,
I want to have 2 types of layout: single column and 2 column with random sized sub-images. Can you help me with that?

@jayeshBaviskar
Copy link

image
How can we resize the height of the images so they scale properly without leaving black backgrounds?
I am using images of size 720 X 480

@morawi
Copy link

morawi commented Oct 5, 2018

Thanks a bunch.

@morawi
Copy link

morawi commented Oct 5, 2018

I've created a function to stitch an arbitrary number of images. I am suing STL10 dataset from torchvision. So, please change the folder_of_data = '/home/morawi/data' to the one you are using and off you go.
https://gist.github.com/morawi/4879f4a8c68c1a51056a25e565b80ccd

@hasansalimkanmaz
Copy link

thank you very much. it really helps

@hemangjoshi37a
Copy link

Very good..

@Bobingstern
Copy link

Not all heroes wear capes.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment