This article mainly introduces how python converts text to image output. it involves Python's skills in text and image operations, which is of great practical value, for more information about how to convert text to image output in python, see the following example. Share it with you for your reference. The specific implementation method is as follows:
#-*-Coding: UTF-8-*-from PIL import Image, ImageFont, ImageDrawtext = U' welcome, 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 + wordline_height = font. getsize (text) [1] img_height = line_height * (len (lines) + 1) print 'Len = ', len (lines) print 'lines =', linesim = Image. new ("RGB", (444, img_height), (255,255,255) dr = ImageDraw. draw (im) x, y = 5, 5for line in lines: dr. text (x, y), line, font = font, fill = "#000000") y + = line_heightim.save ("1.1.jpg ")
I hope this article will help you with Python programming.