Python converts text to image output

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.