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 14, 2020 in Python by Shantanu
• 140 points
2,310 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, 2020 by Gitika
• 65,770 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 24, 2018 in Python by Aryya
• 500 points
822 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
953 views
0 votes
2 answers

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

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
23,871 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
6,253 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
• 3,050 points
9,316 views
0 votes
1 answer

How to send image to firebase using python in raspberry pi?

Here's a code snippet demonstrating the same. # ...READ MORE

answered Jun 23, 2020 in Python by Sam
• 6,260 points
3,289 views
0 votes
1 answer

How to save an image locally using Python in which we already know the URL address?

Hi, @Roshni, The following code is used to ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,770 points
1,915 views
0 votes
1 answer

How to a write reg expression that confirms an email id using the python reg expression module “re”?

Hey, @Roshni, Python has a regular expression module ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,770 points
946 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP