[Python] python将文本转换成图片输出 →→→→→进入此内容的聊天室

来自 , 2020-12-24, 写在 Python, 查看 105 次.
URL http://www.code666.cn/view/6c752ceb
  1. #-*- coding:utf-8 -*-
  2.  
  3.  
  4. from PIL import Image,ImageFont,ImageDraw
  5.  
  6. text = u'欢迎访问脚本分享网,http://www.sharejs.com'
  7.  
  8.  
  9. font = ImageFont.truetype("msyh.ttf",18)
  10.  
  11. lines = []
  12. line =''
  13.  
  14.  
  15. for word in text.split():
  16.     print word
  17.     if font.getsize(line+word)[0] >= 300:
  18.         lines.append(line)
  19.         line = u''
  20.         line += word
  21.         print 'size=',font.getsize(line+word)[0]
  22.     else:
  23.         line = line + word
  24.  
  25.  
  26.  
  27. line_height = font.getsize(text)[1]
  28. img_height = line_height*(len(lines)+1)
  29.  
  30.  
  31. print 'len=',len(lines)
  32. print 'lines=',lines
  33.  
  34. im = Image.new("RGB",(444,img_height),(255,255,255))
  35. dr = ImageDraw.Draw(im)
  36.  
  37. x,y=5,5
  38. for line in lines:
  39.     dr.text((x,y),line,font=font,fill="#000000")
  40.     y += line_height
  41.    
  42.  
  43.  
  44. im.save("1.1.jpg")
  45. #//python/8227

回复 "python将文本转换成图片输出"

这儿你可以回复上面这条便签

captcha