Want to know how to write paragraph on image using pillow python?

+1 vote
I want to write text on image using pillow

Which has width and height 500,500

My text is bigger and that is going out of the image
I want to to align the text at center
I done this using w/2,  h/2

The text come to center but going out the image as it is paragraph

I want to adjust text and when the size of text gone to width 400, then print on next line on same image.

Also want to know if there is any align center option

This is my code

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import textwrap

with open(".txt", "r") as f:
    W, H = (500,500)
    for line in f:
        singleline = (line)
        img = Image.open("bb1.jpg")
        draw = ImageDraw.Draw(img)
        font = ImageFont.truetype('demo2.ttf',20)
        l = textwrap.wrap(singleline, width=40)
        w, h = draw.textsize(singleline)
        draw.text(((W)/2,(H)/2),singleline,font=font, fill = 'black')
        img.save(singleline+'.jpg')
Apr 13 in Python by Shantanu
• 140 points
77 views

1 answer to this question.

0 votes

Hi, @Shantanu,

You can make use of the below shown. Just check what font type and size is appropriate for you and use the following function to change font values:

# font = ImageFont.truetype(<font-file>, <font-size>)
# font-file should be present in provided path.
font = ImageFont.truetype("sans-serif.ttf", 16)
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw 

img = Image.open("sample_in.jpg")
draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(<font-file>, <font-size>)
font = ImageFont.truetype("sans-serif.ttf", 16)
# draw.text((x, y),"Sample Text",(r,g,b))
draw.text((0, 0),"Sample Text",(255,255,255),font=font)
img.save('sample-out.jpg')

You might need to put some extra effort to calculate font size. In case you want to change it based on amount of text user has provided in TextArea.

answered Apr 14 by Gitika
• 29,290 points

Related Questions In Python

0 votes
0 answers

How to mix read() and write() on Python files in Windows

It appears that a write() immediately following a read() on a ...READ MORE

Oct 23, 2018 in Python by Aryya
• 500 points
100 views
0 votes
1 answer

How can I write code to find a palindrome in python without using string functions?

In Logic 1, try if i<int(length/2): instead of if i<int((length/2+1)): In ...READ MORE

answered Jan 16, 2019 in Python by charlie_brown
• 7,760 points
6,508 views
0 votes
0 answers

how to deploy bluk python yamls on kubernetes using jinja template

1.how to deploy  python  services yamls  or ...READ MORE

Mar 19, 2019 in Python by devops learner
208 views
0 votes
1 answer

How can I write a program to add two numbers using functions in python?

def add(a,b):      return a + b #when i call ...READ MORE

answered May 20, 2019 in Python by Mohammad
• 3,230 points
4,257 views
+1 vote
2 answers

How to automate image upload from my computer to site using python-selenium?

Hey, this code worked for me: image_path=os.path.abspath('.\\folder1\\subfolder2\file1.jpg') driver.find_element_by_id("Id of ...READ MORE

answered Aug 19, 2019 in Python by Rishi
735 views
+1 vote
1 answer

How to install tensorflow using anaconda and python 3.7.1 on windows?

Since I am using python 3.5 so ...READ MORE

answered Aug 19, 2019 in Python by Arvind
• 2,980 points
3,446 views
+2 votes
2 answers

How to make a laplacian pyramid using OpenCV python?

down voteacceptTheeThe problem is that you're iterating ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,760 points
2,328 views
0 votes
1 answer
0 votes
1 answer

How to install WordCloud or an equivalent package on Anaconda Python 3.7?

Hey, @Jhonny, Could you please post how did ...READ MORE

answered May 5 in Python by Gitika
• 29,290 points
56 views