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:
Python | OCR on All the Images present in a Folder Simultaneously
Next article icon

Python Pillow – Writing Text on Image

Last Updated : 13 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see how to write text on images with the Python Pillow module.

Installation

This module is not preloaded with Python. So to install it execute the following command in the command-line:

pip install pillow

Stepwise implementation:

Step 1: Import Pillow library

To complete this task the functions required from the Pillow library are: Image, ImageDraw, ImageFont. All these functions are imported as:

from PIL import Image, ImageDraw, ImageFont

Step 2: Open an image

In this step, the image on which we are going to add text is imported and open by using the “Image.open(‘Image_name’)”. In the given case the gfg logo is used to add the text on it. The image name is gfg_logo.jpeg. So, it is written as:

img = Image.open(‘gfg_logo.jpeg’)

Step 3: Image Conversion

In this step, we convert our image into an editable format by using “ImageDraw.Draw(‘variable used to store the image in above step’)”. In the given case this is written as:

d1 = ImageDraw.Draw(img)

Step 4: Font style.

This step is optional. It is for those who want their text to look cool or stylish because someone won’t select any font style then the system takes the default font style. At first download the font style file from https://ttfonts.net/font/1004_Mistral.htm . After downloading the file use the function ImageFont.truetype(‘adddress_of_font_style’, font_style). In given cases this is written as:

myFont = ImageFont.truetype(‘/home/raghav/PycharmProjects/gfg/Mistral.ttf’, 20)

Step 5: Render the text

This is the main step in which the whole properties of the font get decided. This is written as:

d1.text((65, 10), “Sample text”, fill =(255, 0, 0),font=myFont)

  • Starting Coordinates: Pillow library uses a Cartesian pixel coordinate system, with (0,0) in the upper left corner.
  • Text: String between single or double quotations
  • Text color in RGB format: For the color, you want you can check it to Google for its RGB color code and use.
  • Font style: Download the font from Google and use it.

Step 6: Show and Save result.

The final step is to display the modified image on the screen for this img.show() function is used and to store the image img.save(“results.jpeg”) is used. 

Below is the implementation:

Input Image

Python3

# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
 
# gfg_logo.jpeg image opened using open
# function and assigned to variable named img
img = Image.open('gfg_logo.jpeg')
 
# Image is converted into editable form using
# Draw function and assigned to d1
d1 = ImageDraw.Draw(img)
 
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/Mistral.ttf', 20)
 
# Decide the text location, color and font
d1.text((65, 10), "Sample text", fill =(255, 0, 0),font=myFont)
 
# show and save the image
img.show()
img.save("results.jpeg")
                      
                       

 

 

Output: 


 

Image after modification


 

Example 1: To change the location of the text change the coordinates in step 5.


 

The dimensions are changed from (65,100) to (65,170). So, the location of text goes down because from (x,y) coordinates the value of y-coordinate gets increased as shown in the output.


 

Python3

# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
 
# gfg_logo.jpeg image opened using
# open function and assigned to variable named img
img = Image.open('gfg_logo.jpeg')
 
# Image is converted into editable form using
# Draw function and assigned to d1
d1 = ImageDraw.Draw(img)
 
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/Mistral.ttf', 20)
 
# Decide the text location, color and font
d1.text((65, 170), "Sample text", fill =(255, 0, 0),font=myFont)
 
# show and save the image
img.show()
img.save("results.jpeg")
                      
                       

 

 

Output: 


 

Image after changing it’s coordinates


 

Example 2: To change the color of the text again change in step 5.


 

There are RGB colour code on which Pillow work (R,G,B) R  for RED, G for GREEN, B for Blue. In above case the R and B value is 0 and G value is 255 i.e. Maximum. So, the colour of text changes to green as shown in output.


 

Python3

# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
 
# gfg_logo.jpeg image opened using
# open function and assigned to variable named img
img = Image.open('gfg_logo.jpeg')
 
# Image is converted into editable form using Draw function
# and assigned to d1
d1 = ImageDraw.Draw(img)
 
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/Mistral.ttf', 20)
 
# Decide the text location, color and font
d1.text((65, 170), "Sample text", fill =(0, 255, 0),font=myFont)
 
# show and save the image
img.show()
img.save("results.jpeg")
                      
                       

 

 

Output: 


 

Image after changing colour of text


 

Example 3: To change the text change the text field in step 5.


 

Python3

# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
 
# gfg_logo.jpeg image opened using open function
# and assigned to variable named img
img = Image.open('gfg_logo.jpeg')
 
# Image is converted into editable form using Draw
# function and assigned to d1
d1 = ImageDraw.Draw(img)
 
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/Mistral.ttf', 20)
 
# Decide the text location, color and font
d1.text((65, 170), "Sample text 2", fill=(0, 255, 0), font=myFont)
 
# show and save the image
img.show()
img.save("results.jpeg")
                      
                       

Output: 

Image after changing text field

Example 4: To change the size of the text go to step 4 and change the size.

Python3

# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
 
# gfg_logo.jpeg image opened using
# open function and assigned to variable named img
img = Image.open('gfg_logo.jpeg')
 
# Image is converted into editable form using
# Draw function and assigned to d1
d1 = ImageDraw.Draw(img)
 
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/Mistral.ttf', 30)
 
# Decide the text location, color and font
d1.text((65, 170), "Sample text", fill = (0, 255, 0),font=myFont)
 
# show and save the image
img.show()
img.save("results.jpeg")
                      
                       

 

 

Output: 


 

Image after changing text size


 

Example 5: To change the text font go to step 4.


 

Python3

# Import Image for basic functionalities like open, save, show
# Import ImageDraw to convert image into editable format
# Import ImageFont to choose the font style
from PIL import Image, ImageDraw, ImageFont
 
# gfg_logo.jpeg image opened using open function and
# assigned to variable named img
img = Image.open('gfg_logo.jpeg')
 
# Image is converted into editable form using Draw
# function and assigned to d1
d1 = ImageDraw.Draw(img)
 
# Font selection from the downloaded file
myFont = ImageFont.truetype('/home/raghav/PycharmProjects/gfg/00006_44s.ttf', 30)
 
# Decide the text location, color and font
d1.text((0, 170), "Sample text", fill =(0, 255, 0),font=myFont)
 
# show and save the image
img.show()
img.save("results.jpeg")
                      
                       

 

 

Output:


 

Image after changing font style


 



Next Article
Python | OCR on All the Images present in a Folder Simultaneously
author
parasharraghav
Improve
Article Tags :
  • Python
  • Python-pil
Practice Tags :
  • python

Similar Reads

  • Python Pillow Tutorial
    sinceDigital Image processing means processing the image digitally with the help of a computer. Using image processing we can perform operations like enhancing the image, blurring the image, extracting text from images, and many more operations. There are various ways to process images digitally. He
    15+ min read
  • Introduction to Pillow

    • Python: Pillow (a fork of PIL)
      Python Imaging Library (expansion of PIL) is the de facto image processing package for Python language. It incorporates lightweight image processing tools that aids in editing, creating and saving images. Support for Python Imaging Library got discontinued in 2011, but a project named pillow forked
      4 min read

    Installation and setup

    • How to Install Pillow on MacOS?
      In this article, we will learn how to install Pillow in Python on MacOS. Python Imaging Library (expansion of PIL) is the de facto image processing package for Python language. Installation:Method 1: Using pip to install Pillow Follow the below steps to install the Pillow package on macOS using pip:
      2 min read

    • How to Install PIL on Windows?
      In this article, we will look into the various methods of installing the PIL package on a Windows machine. Prerequisite:Python PIP or Ananconda (Depending upon your preference)For PIP Users: Open up the command prompt and use the below command to install the PIL package: pip install Pillow The follo
      1 min read

    • How to Install PIL on Linux?
      PIL is an acronym for Python Image Library. It is also called Pillow. It is one of the most famous libraries for manipulating images using the python programming language. It is a free and open-source Python library. Installing PIL on Linux:Method 1: Using PIP command: Step 1: Open up the Linux term
      1 min read

    Loading and Saving Images

    • Python PIL | Image.save() method
      PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files,
      3 min read

    • Python PIL | Image.show() method
      PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files,
      1 min read

    • Finding Difference between Images using PIL
      Python interpreter in itself doesn't contain the ability to process images and making out a conclusion to it. So, PIL(Python Imaging Library) adds image processing powers to the interpreter. PIL is an open-source library that provides python with external file support and efficiency to process image
      2 min read

    Image Manipulation Basics

    • Python Pillow - Working with Images
      In this article, we will see how to work with images using Pillow in Python. We will discuss basic operations like creating, saving, rotating images. So let's get started discussing in detail but first, let's see how to install pillow. Installation To install this package type the below command in t
      4 min read

    • Python PIL | Image.resize() method
      PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files,
      4 min read

    • Python Pillow - Flip and Rotate Images
      Prerequisites: Pillow Python Pillow or PIL is the Python library that provides image editing and manipulating features. The Image Module in it provides a number of functions to flip and rotate images. image.transpose() is the function used to rotate and flip images with necessary keywords as paramet
      2 min read

    • Python PIL | paste() and rotate() method
      PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.Image.paste() method is used to paste an image on another image. This is where the new() method comes in handy. Syntax: PIL.Image.Image.paste(image_1, image_2, box=None, mask=None) OR i
      2 min read

    Adjusting Image Properties

    • 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

    • Image Enhancement in PIL
      The Python Imaging Library(PIL) adds powerful image processing capabilities. It provides immense file format support, an efficient representation, and fairly powerful image processing capabilities. The core image library is intended for fast access to data stored in very few basic pixel formats. It
      4 min read

    Image Filtering and Effects

    • Python Pillow - Blur an Image
      Blurring an image is a process of reducing the level of noise in the image, and it is one of the important aspects of image processing. In this article, we will learn to blur an image using a pillow library. To blur an image we make use of some methods of ImageFilter class of this library on image o
      2 min read

    • How to merge images with same size using the Python 3 module pillow?
      In this article, the task is to merge image with size using the module pillow in python 3.  Python 3 module pillow : This is the update of Python Imaging Library. It is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and savi
      2 min read

    Drawing on Images

    • Adding Text on Image using Python - PIL
      In Python to open an image, image editing, saving that image in different formats one additional library called Python Imaging Library (PIL). Using this PIL we can do so many operations on images like create a new Image, edit an existing image, rotate an image, etc. For adding text we have to follow
      2 min read

    • Python Pillow - ImageDraw Module
      Python's Pillow which is a fork of the discontinued Python Imaging Library (PIL) is a powerful library that is capable of adding image processing capabilities to your python code. Pillow offers many modules that ease the process of working and modifying images. In this article, we will have a look a
      5 min read

    • Python Pillow - Colors on an Image
      In this article, we will learn Colors on an Image using the Pillow module in Python. Let's discuss some concepts: A crucial class within the Python Imaging Library is the Image class. It's defined within the Image module and provides a PIL image on which manipulation operations are often administere
      4 min read

    Image Transformations

    • How to rotate an image using Python?
      In this article, let's see how to rotate an Image using Python. By Image Rotation, the image is rotated about its center by a specified number of degrees. The rotation of an image is a geometric transformation. It can be done either by Forward Transformation (or) Inverse Transformation. Here Image P
      2 min read

    • Python PIL | Image.transform() method
      PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files,
      1 min read

    Working with Image Metadata

    • How to extract image metadata in Python?
      Prerequisites: PIL Metadata stands for data about data. In case of images, metadata means details about the image and its production. Some metadata is generated automatically by the capturing device.  Some details contained by image metadata is as follows: HeightWidthDate and TimeModel etc. Python h
      2 min read

    • Python | Working with the Image Data Type in pillow
      In this article, we will look into some attributes of an Image object that will give information about the image and the file it was loaded from. For this, we will need to import image module from pillow. Image we will be working on : size() method - It helps to get the dimensions of an image. IMG =
      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