I am trying to draw text (which can be of any length depending on what the user enters) on a blank image. I need to split the text in new lines to avoid creating too large images, and I also need to create an image with a size related to how many characters the user enters, avoiding white space. This is what I came up with:
import PIL
import textwrap
from PIL import ImageFont, Image, ImageDraw
usrInput = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet scelerisque nulla. Pellentesque mollis tellus ut arcu malesuada auctor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Ut tristique purus non ultricies vulputate"
text = textwrap.fill(usrInput,50)
fontColor = (255,255,255)
fontSize = 40
font = ImageFont.truetype("/System/Library/Fonts/AppleGothic.ttf",fontSize)
background = (200,255,0)
imgSize = ImageDraw.Draw.textsize(text, font)
def CreateImg ():
img = Image.new("RGBA", imgSize, background)
draw = ImageDraw.Draw(img)
draw.text((0,0), text, fontColor, font)
img.save("test.png")
CreateImg()
. font.getsize, , , , , . , .
, , ImageDraw.Draw.textsize( ImageDraw.Draw.multiline_textsize, ), :
Traceback (most recent call last):
File "pil.py", line 17, in <module>
imgSize = ImageDraw.Draw.textsize(text, font)
AttributeError: 'function' object has no attribute 'textsize'
? ?