- PIL Home
- Python Image Library Intro
- Python Image Library Setup
- Python Image Library Basics
- Python Image Library Hierarchy
- Python Image Library Modules
- Python ImageColor Module
- Python ImageDraw Module
- Python ImageStat Module
- Python ImageTK Module
- Python ImageFile Module
- Python ImageFilter Module
- Python ImageGrab Module
- Python ImageMath Module
- Python - Loading Image
- Python - Rotating Image
- Python - Blurring Image
- Python - Converting Image
- Python - Cropping Image
- Python - Image Thumbnails
- Python - Text on Images
- Python - Multiple Image Copies
- Python - Saving Image As PDF
- Python - Saving Screenshot
- Python - Resizing Image
- Python - Adding Watermark
- Python Image Library Resources
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python - Text on Images
Description
In order to place some text on images, we generally use any of the image editor software.Python combining with Image Library has the functionality to write text on the image.For writing the text on image, we require the Image,ImageFont and ImageDraw modules
Image module :Image is the fundamental module for loading and opening the image and ImageGrab module is useful for taking the screenshot of the screen.
ImageDraw module :The ImageDraw module provide simple 2D graphics for Image objects. You can use this module to create new images, annotate or retouch existing images, and to generate graphics on the fly for web use
ImageFont module :ImageFont module is used to capture current date and time or the previous.
First import the Image module and ImageFilter modules and then open the image with open method in Image.ImageFilter contains the various attributes that are used to blue the image.Make sure to give the complete path of the image where it has been placed.
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
flowers = Image.open("flowers.jpg") img=Image.open("flower1.jpg")
from PIL import ImageFont
from PIL import ImageDraw
flowers = Image.open("flowers.jpg") img=Image.open("flower1.jpg")
Create an object for ImageFont class with truetype method and object for ImageDraw class with Draw method as below.Make sure that the complete path of the font is been provided. In windows fonts are been defined in C:\Windows\Fonts
font = ImageFont.truetype("arialbd.ttf",40)
draw = ImageDraw.Draw(img)
draw = ImageDraw.Draw(img)
draw.text((0,0),"This is a test",(255,130,0),font=font1)
Syntax
Following is the syntax of functions been used.
# Import Pillow:
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
# Load the original image:
object = Image.open(image)
font = ImageFont.truetype(font,size)
draw = ImageDraw.Draw(image)
draw.text((text position),"text to be written on image",(coordinates),font=font)
object.save(image)
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
# Load the original image:
object = Image.open(image)
font = ImageFont.truetype(font,size)
draw = ImageDraw.Draw(image)
draw.text((text position),"text to be written on image",(coordinates),font=font)
object.save(image)
Parameters
For the truetype method of ImageFont library, we will be passing 2 arguments1. font
2. size
Note: Make sure that the font you are defining is available in C:\Windows\Fonts directory
Call the text method for the instance that has been created for ImageDraw module.It contains 4 arguments.
1st argument is the coordinate to place the text
2nd argument is the actual text that has to be displayed
3rd argument is the color of the text in terms of RGB
4th argument is the font object which contains the type of the font
Note:If image is in same directory where your python program resides, you can give the image name itself. If the image resides in some other directory, you are supposed to define the complete path of the image.
Return value
The new image with text on i will be saved to the local directory with the save method.Example
Here is the complete program to display text on image at the defined coordinates.
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
img=Image.open("flower1.jpg")
font = ImageFont.truetype("arialbd.ttf",40)
draw = ImageDraw.Draw(img)
draw.text((0,0),"This is a test",(255,130,0),font=font)
final_image = "text_image.png"
img.show(final_image)
img.save(final_image)
from PIL import ImageFont
from PIL import ImageDraw
img=Image.open("flower1.jpg")
font = ImageFont.truetype("arialbd.ttf",40)
draw = ImageDraw.Draw(img)
draw.text((0,0),"This is a test",(255,130,0),font=font)
final_image = "text_image.png"
img.show(final_image)
img.save(final_image)
Before
After
Advertisements