SoFunction
Updated on 2024-12-15

python convert text to image output method

This article example describes the method of python to convert text to image output. Shared for your reference. Specific implementation methods are as follows:

#-*- coding:utf-8 -*-
from PIL import Image,ImageFont,ImageDraw
text = u'Welcome to me,https://'
font = ("",18)
lines = []
line =''
for word in ():
  print word
  if (line+word)[0] >= 300:
    (line)
    line = u''
    line += word 
    print 'size=',(line+word)[0]
  else:
    line = line + word
line_height = (text)[1]
img_height = line_height*(len(lines)+1)
print 'len=',len(lines)
print 'lines=',lines
im = ("RGB",(444,img_height),(255,255,255))
dr = (im)
x,y=5,5
for line in lines:
  ((x,y),line,font=font,fill="#000000")
  y += line_height
("1.")

I hope that what I have said in this article will help you in your Python programming.