python将文本转换成图片输出的方法

WBOY
發布: 2016-06-06 11:26:09
原創
2379 人瀏覽過

本文实例讲述了python将文本转换成图片输出的方法。分享给大家供大家参考。具体实现方法如下:

#-*- coding:utf-8 -*-
from PIL import Image,ImageFont,ImageDraw
text = u'欢迎访问,http://www.bitsCN.com'
font = ImageFont.truetype("msyh.ttf",18)
lines = []
line =''
for word in text.split():
  print word
  if font.getsize(line+word)[0] >= 300:
    lines.append(line)
    line = u''
    line += word 
    print 'size=',font.getsize(line+word)[0]
  else:
    line = line + word
line_height = font.getsize(text)[1]
img_height = line_height*(len(lines)+1)
print 'len=',len(lines)
print 'lines=',lines
im = Image.new("RGB",(444,img_height),(255,255,255))
dr = ImageDraw.Draw(im)
x,y=5,5
for line in lines:
  dr.text((x,y),line,font=font,fill="#000000")
  y += line_height
im.save("1.1.jpg")
登入後複製

希望本文所述对大家的Python程序设计有所帮助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!