Python PIL | ImageDraw.Draw.text()
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. 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.
ImageDraw.Draw.text() Draws the string at the given position.
Syntax:
ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor=None, spacing=0, align=”left”)
Parameters:
xy – Top left corner of the text.
text – Text to be drawn. If it contains any newline characters, the text is passed on to multiline_text()
fill – Color to use for the text.
font – An ImageFont instance.
spacing – If the text is passed on to multiline_text(), the number of pixels between lines.
align – If the text is passed on to multiline_text(), “left”, “center” or “right”.Return Type:
returns an image with text.
Image Used:
Code : Using PIL | ImageDraw.Draw.text()
# Importing Image and ImageFont, ImageDraw module from PIL package from PIL import Image, ImageFont, ImageDraw # creating a image object image = Image.open(r'C:\Users\System-Pc\Desktop\rose.jpg') draw = ImageDraw.Draw(image) # specified font size font = ImageFont.truetype(r'C:\Users\System-Pc\Desktop\arial.ttf', 20) text = 'LAUGHING IS THE \n BEST MEDICINE' # drawing text size draw.text((5, 5), text, font = font, align ="left") image.show() |
Output:

Another Example:Here we changing parameter.
Image Used:

Code : Using PIL | ImageDraw.Draw.text()
# Importing Image and ImageFont, ImageDraw module from PIL package from PIL import Image, ImageFont, ImageDraw # creating a image object image = Image.open(r'C:\Users\System-Pc\Desktop\flower.jpg') draw = ImageDraw.Draw(image) # specified font size font = ImageFont.truetype(r'C:\Users\System-Pc\Desktop\arial.ttf', 20) text = 'LAUGHING IS THE \n BEST MEDICINE' # drawing text size draw.text((5, 5), text, fill ="red", font = font, align ="right") image.show() |
Output:

Recommended Posts:
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Send mail from your Gmail account using Python
- Python – The new generation Language
- Print Single and Multiple variable in Python
- Increment and Decrement Operators in Python
- str() vs repr() in Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Generate all permutation of a set in Python
- Class or Static Variables in Python
- trunc() in Python
- Division Operators in Python
- Interesting facts about strings in Python | Set 1
- When to use yield instead of return in Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



