我正在尝试在空白图像上绘制文本(根据用户输入的内容可以是任意长度)。我需要在换行符中拆分文本以避免创建太大的图像,我还需要创建一个图像,其大小与用户输入的字符数有关,避免出现任何空白空间。这是我到目前为止想出的:
import PIL
import textwrap
from PIL import ImageFont, Image, ImageDraw
#Input text
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)
#font size, color and type
fontColor = (255,255,255)
fontSize = 40
font = ImageFont.truetype("/System/Library/Fonts/AppleGothic.ttf",fontSize)
#Image size and background color
background = (200,255,0)
#imgSize …Run Code Online (Sandbox Code Playgroud)