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 the given approach.
- Import module
- Open targeted image
- Add text property using image object
- Show that edited Image
- Save that image
Syntax: obj.text( (x,y), Text, font, fill)
- (x, y): This X and Y denotes the starting position(in pixels)/coordinate of adding the text on an image.
- Text: A Text or message that we want to add to the Image.
- Font: specific font type and font size that you want to give to the text.
- Fill: Fill is for to give the Font color to your text.
Other than these we required some module from PIL to perform this task. We need ImageDraw that can add 2D graphics ( shapes, text) to an image. Also, we required the ImageFont module to add custom font style and font size. Given below is the Implementation of add text to an image.
Example 1: Add a simple text to an image. ( without custom Font style)
Here You can see that we successfully add text to an image but it not properly visible so we can add the Font parameter to give a custom style.
Example 2: Add a simple text to an image. ( With custom Font style)
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course