Python generates CAPTCHA, text converted to picture

Source: Internet
Author: User

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

Related Article

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.