Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Open CV
  • scikit-image
  • pycairo
  • Pyglet
  • Python
  • Numpy
  • Pandas
  • Python Database
  • Data Science With Python
  • Machine Learning with Python
  • Django
  • Flask
  • R
Open In App
Next Article:
How to add text on an image using pillow in Python ?
Next article icon

How to Concatenate image using Pillow in Python ?

Last Updated : 07 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisites: Python Pillow

Concatenate image means joining of two images. We can merge any image whether it has different pixels, different image formats namely, ‘jpeg’, ‘png’, ‘gif’, ‘tiff’, etc. In python, we can join two images using the Python image library also known as the pillow library. In this article, we will see how the concatenation of images is done.

Concatenation of images can be done in two ways :

  • Horizontal
  • Vertical

Concatenating images horizontally

Approach:

  • Import module
  • Open the images
  • Resize the image using Resize() function. Both the resize images should be of the same width and height so that their aspect ratio is intact and can be pasted into the new background image.
  • To create a new image it has a new() function which has 3 parameters (“mode”,(size),color)
  • Paste the image to new image using paste()

Program:

Python3

# library
from PIL import Image
import matplotlib.pyplot as plt
  
# opening up of images
img = Image.open("logo.png")
img1 = Image.open("logo2.png")
img.size
img1.size
img_size = img.resize((250, 90))
img1_size = img1.resize((250, 90))
  
# creating a new image and pasting 
# the images
img2 = Image.new("RGB", (500, 90), "white")
  
# pasting the first image (image_name,
# (position))
img2.paste(img_size, (0, 0))
  
# pasting the second image (image_name,
# (position))
img2.paste(img1_size, (250, 0))
  
plt.imshow(img2)
                      
                       

 Output :

Concatenating images vertically

The whole code is the same as horizontal but the only change is that in horizontal we double the width and the height is same but in vertical we make the size of width same, but we double the height. 

Approach:

  • Import the libraries for image processing.
  • Use Image.open() to open the library.
  • Use img.size to know the size of the image.
  • Use img.resize((width,height)) to resize the image.
  • Both the images should be of same size.
  • Create a new image using new() and pass the 3 parameters”mode”,size,”color”).
  • Size in new image should be (width, 2*height).
  • After the creation of new image, paste the first image by using paste() and pass the parameter(img_resize,(position)) #position(0,0)
  • After pasting the first image, paste the second image using paste and pass the parameter(img1_reszie, (position)). In position, width would be the same but the height would be the last position of the first image’s height. . #position=(0,180)
  • Use plt.imshow(img2) to show the concatenate of images.

Program:

Python3

# library
from PIL import Image
import matplotlib.pyplot as plt
  
  
# opening up of images
img = Image.open("logo.png")
img1 = Image.open("logo2.png")
img.size
img1.size
img_size = img.resize((250, 90))
img1_size = img1.resize((250, 90))
  
# creating a new image and pasting the 
# images
img2 = Image.new("RGB", (250, 180), "white")
  
# pasting the first image (image_name,
# (position))
img2.paste(img_size, (0, 0))
  
# pasting the second image (image_name,
# (position))
img2.paste(img1_size, (0, 90)
  
plt.imshow(img2)
                      
                       

Output:



Next Article
How to add text on an image using pillow in Python ?

K

kanishkmadan2000
Improve
Article Tags :
  • Python
  • Python-pil
Practice Tags :
  • python

Similar Reads

  • Concatenate images using OpenCV in Python
    To concatenate images vertically and horizontally with Python, cv2 library comes with two functions as: hconcat(): It is used as cv2.hconcat() to concatenate images horizontally. Here h means horizontal.vconcat(): It is used as cv2.vconcat() to concatenate images vertically. Here v means vertical.Im
    3 min read
  • How to add text on an image using pillow in Python ?
    Prerequisites: PIL In this article we'll see how we can add text on an image using the pillow library in Python. Python Imaging Library (PIL) is the de facto image processing package for Python language. It incorporates lightweight image processing tools that aids in editing, creating and saving ima
    2 min read
  • Change image resolution using Pillow in Python
    Prerequisites: Python pillow PIL is the Python Imaging Library which provides the python interpreter with an in-depth file format support, an efficient internal representation, and fairly powerful image processing capabilities. Changing the resolution of an image simply means reducing or increasing
    2 min read
  • How to Convert Image to PDF in Python?
    img2pdf is an open source Python package to convert images to pdf format. It includes another module Pillow which can also be used to enhance image (Brightness, contrast and other things) Use this command to install the packages pip install img2pdf Below is the implementation: Image can be converted
    1 min read
  • Python | Crop image using pillow
    In this article, we will learn to crop an image using pillow library. Cropping an image means to select a rectangular region inside an image and removing everything outside the rectangle. To crop an image we make use of crop() method on image objects. Syntax : IMG.crop(box_tuple) Parameters : Image_
    1 min read
  • Convert PDF to Image using Python
    Many tools are available on the internet for converting a PDF to an image. In this article, we are going to write code for converting pdf to image and make a handy application in python. Before writing the code we need to install the required module pdf2image and poppler. Modules Neededpdf2image 1.1
    2 min read
  • Convert an image into jpg format using Pillow in Python
    Let us see how to convert an image into jpg format in Python. The size of png is larger when compared to jpg format. We also know that some applications might ask for images of smaller sizes. Hence conversion from png(larger ) to jpg(smaller) is needed. For this task we will be using the Image.conve
    2 min read
  • Animate image using OpenCV in Python
    In this article, we will discuss how to animate an image using python's OpenCV module. Let's suppose we have one image. Using that single image we will animate it in such a way it will appear continuous array of the same image. This is useful in animating the background in certain games. For example
    3 min read
  • How to create Word Art from an image using Python?
    In this article, we are going to learn how to create word art from an image using Python. In this, we are going to take an image as input and then created a similar image in a text form using the characters. We can perform this task with the help of pillow and pywhatkit modules of Python. Pillow Thi
    2 min read
  • How to rotate an image by an angle using PyTorch in Python?
    In this article, we are going to see how to rotate an image by an angle in PyTorch. To achieve this, we can use RandomRotation() method. RandomRotation() transform accepts both PIL and tensor images. A Tensor Image is a tensor with (C, H, W) shape, C is for the number of channels, H and W are for th
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences