Convert text to image font in python,

Source: Internet
Author: User

Convert text to image font in python,

The previous blog post mentioned how to get a Chinese Character Font. This article describes how to convert words into images one by one, which is useful in machine learning.

In a word, use pygame to render text to the image.

The code below.

Import osimport pygamechinese_dir = 'China' if not OS. path. exists (chinese_dir): OS. mkdir (chinese_dir) pygame. init () start, end = (0x4E00, 0x9FA5) # Chinese character encoding range: for codepoint in range (int (start), int (end): word = unichr (codepoint) font = pygame. font. font ("msyh. ttc ", 22) # The current directory must contain the font file msyh of. or go to the c: \ Windows \ Fonts directory to find rtext = font. render (word, True, (0, 0, 0), (255,255,255) pygame. image. save (rtext, OS. path. join (chinese_dir, word + ". png "))

The following figure shows the effect.





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.