The Wayback Machine - https://web.archive.org/web/20241126100405/https://www.geeksforgeeks.org/python-pillow-writing-text-on-image/
Open In App

Python Pillow – Writing Text on Image

Last Updated : 13 Jul, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

 



Previous Article
Next Article

Similar Reads

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 images. Pillow supports numerous image file formats i
2 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_path- Location of the image IMG- Image to crop box
1 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 = Image.open(Image_path) croppedIm = IMG.size # imp
2 min read
Floodfill Image using Python-Pillow
Seed Fill also known as flood fill, is an algorithm used to identify connected paths in a definite enclosed region. The algorithm has an array of practical applications, such as - Optimized pathfindingPaint Bucket Tool a generic tool found in several image processing packages, uses the algorithm internallyMazesolving uses floodfill (paired with tra
3 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.convert() method of the Pillow module. Algorithm : Impo
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 the number of pixels in an image, without changing
2 min read
Change the ratio between width and height of an image using Python - Pillow
Python Imaging Library (expansion of PIL) is the de facto image processing package for the Python language. It incorporates lightweight image processing tools that aid in editing, creating, and saving images. This module is not preloaded with Python. So to install it execute the following command in the command-line: pip install pillowTo change the
2 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 administered. An instance of this class is often created in s
4 min read
Create transparent png image with Python - Pillow
To create a transparent png using Python3, the Pillow library is used. The Pillow library comes with python itself. If python is unable to find Pillow library then open the command prompt and run this command:- pip install Pillow Note: If you got any problem installing Pillow using pip, then install and setup pip first. For this check this article.
3 min read
Add padding to the image with Python - Pillow
Prerequisites: Pillow(python library) Padding is the space between the contents, the container, and the border. An image can be considered content to some container and extra padding can be added to it. This article depicts how this can be done. Padding can be added by straightaway supplying the values for the side to be padded. These values can th
2 min read
Generate square or circular thumbnail image with Python - Pillow
Prerequisites: Pillow, Numpy In the word "THUMBNAIL" the word THUMB means short. A thumbnail is the compressed preview image of the original image or a Thumbnail is the smaller version of an image. In simple words, the thumbnail is the smaller image that represents the larger/original image. Usually, the shape of the thumbnail is dependent on the o
6 min read
Python Pillow - Image Sequences
The ImageSequence module in the pillow contains a wrapper class that helps users to iterate over the frames of an image sequence. It can iterate over animations, gifs, etc. Iterator class This class accepts an image object as a parameter. It implements an iterator object that can be used by a user to iterate over an image sequence. [ ] operator can
3 min read
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 objects. Note: The image used to blur in all differ
2 min read
How to Concatenate image using Pillow in Python ?
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 o
3 min read
Python Pillow - Using Image Module
In this article, we will see how to work with Image Module of PIL in Python. First, let's see how to install the PIL. Installation: Linux: On the Linux terminal type the following: pip install Pillow Installing pip via terminal: sudo apt-get update sudo apt-get install python-pipWorking with Image Module Here, we will go through some methods and pr
5 min read
Convert Text Image to Hand Written Text Image using Python
In this article, we are going to see how to convert text images to handwritten text images using PyWhatkit, Pillow, and Tesseract in Python. Module needed: Pytesseract: Sometimes known as Python-tesseract, is a Python-based optical character recognition (OCR) program. It can read and recognize text in photos, license plates, and other documents. To
2 min read
Python - Color Inversion using Pillow
Color Inversion (Image Negative) is the method of inverting pixel values of an image. Image inversion does not depend on the color mode of the image, i.e. inversion works on channel level. When inversion is used on a multi color image (RGB, CMYK etc) then each channel is treated separately, and the final result if formed by calibrating the results
4 min read
Python - Edge Detection using Pillow
Edge Detection, is an Image Processing discipline that incorporates mathematics methods to find edges in a Digital Image. Edge Detection internally works by running a filter/Kernel over a Digital Image, which detects discontinuities in Image regions like stark changes in brightness/Intensity value of pixels. There are two forms of edge detection: S
5 min read
Python - Channel Drop using Pillow
A channel drop is a method of removing one of the channels of a multichannel image. By removing means turning the color value of a particular channel to 0 (all pixels), i.e. that particular channel doesn't have any effect on the final image (assuming colors are blended `normally`). Color theory (Color Wheel) is followed when dropping color channels
5 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 saving many various image file formats. To merge images
2 min read
Create and save animated GIF with Python - Pillow
Prerequisites: pillow In this article, we are going to use a pillow library to create and save gifs. GIFs: The Graphics Interchange Format(gif) is a bitmap image format that was developed by a team at the online services provider CompuServe led by American computer scientist Steve Wilhite on 15 June 1987. A GIF file normally stores a single image,
3 min read
Python Pillow - Creating a Watermark
In this article, we will see how the creation of a watermark is done using the Pillow library in Python. Pillow is a Python Image Library(PIL) that is used for manipulating images and working on the different formats of images like ('jpeg', 'png', 'gif', 'tiff' etc.). There are various image processing you can do using pillow library like resizing,
5 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 parameters. Syntax: image.transpose(appropriate keyword)
2 min read
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 the terminal. pip install pillowCreating new image
4 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 at the ImageDraw module of this library. ImageDraw
5 min read
Convert PNG to ICO with Pillow in Python
In this article, we will convert PNG to ICO using pillow in Python. Convert PNG to ICO with Pillow in Python Before moving further first let us understand what is PNG and ICO. The PNG stands for Portable Network Graphic. It is often used to store web graphics. The png image can be used with transparent background. Whereas, the ICO file format is an
2 min read
Expanding the Canvas Without Resizing in Python Pillow
The Python Imaging Library (PIL) is a powerful tool for image processing in Python, offering a wide range of functionalities from basic image manipulations to complex operations. One of the useful features of PIL is the ability to expand the canvas of an image without resizing the original content. This article explores the concept of expanding the
4 min read
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 the original PIL project and added Python3.x suppo
4 min read
Reading and Writing to text files in Python
Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character ('
9 min read
Convert Text and Text File to PDF using Python
PDFs are one of the most important and widely used digital media. PDF stands for Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system. Converting a given text or a text file to PDF (Portable Document Format) is one of the basic requirements in
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg