Hello all,
I’ve currently been working on a project using inkyPHAT and the buttonshim on a raspberry pi zero. I require text to automatically go to the following line whenever it won’t fit on the screen. Pillow’s draw.multilline_text is not working. Any ideas?
Thanks
What exactly happens? Is it returning a particular error?
It is just displaying text normally, instead of “multi-lining” it
So I’m very confused, because as far as I can see from all of the documentation and examples online the multiline_text
function doesn’t auto-wrap text, so I’m not sure why it is exists if it does more or less what the text
function does.
Fortunately, there is a reasonably quick way to do what you want using the textwrapping
module:
from PIL import Image, ImageDraw
import textwrap
# Create an image to hold some text
img = Image.new("RGB", (300, 200))
# Draw the image so that text can be put on it
img2 = ImageDraw.Draw(img)
# Create the length-wrapped text
wrappedText = textwrap.wrap("A longish string for testing this script", width=15)
# The length-wrapped text is a list, so join it to one string using linebreaks
# (Each element in the list is a new line of text)
joinedText = "\n".join(wrappedText)
# Write the text on the image
img2.multiline_text((5,5), joinedText)
#Show the image
img.show()
The width
argument controls how many characters are on a line before the text is split into a new line. After a few quick tests this produces identical results whether you use multiline_text
or text
, so again, I’m not sure what the purpose of multiline_text
is.
Thank you sooo much. I’ll test this asap