In 58 or market and other sites often see the mobile phone number is a picture format, or some site verification code. These are all dynamically generated, and today we'll look at how to use Python to generate images from text. In fact, today mainly with the help of Pygame image rendering module, this is relatively simple, by the way to help you review the Pygame this game framework. All right, just go to the code.
Environment: python2.7, students with Python3 can also test
#coding: UTF-8
#载入必要的模块
Import OS
Import Pygame
From pygame.locals Import *
#pygame初始化
Pygame.init ()
Text = u "pythontab Chinese net"
#设置字体和字号
Font = Pygame.font.SysFont (' Microsoft Yahei ', 64)
#渲染图片, set the background color and font style, the front color is the font color
Ftext = Font.render (text, True, (65, 83, 130), (255, 255, 255))
#保存图片
Pygame.image.save (Ftext, "d:/pythontab.jpg") #图片保存地址
Note: If you want to generate Chinese fonts in the font settings, you want to set fonts that support Chinese. Otherwise it will generate garbled
Python generates CAPTCHA, text converted to picture