环境:python2.7,装有python3的同学也可以测试一下
- #coding: UTF-8
- #载入必要的模块
- import os
- import pygame
- from pygame.locals import *
- #pygame初始化
- pygame.init()
- text = u"Java开源大全"
- #设置字体和字号
- font = pygame.font.SysFont('Microsoft YaHei', 64)
- #渲染图片,设置背景颜色和字体样式,前面的颜色是字体颜色
- ftext = font.render(text, True, (65, 83, 130),(255, 255, 255))
- #保存图片
- pygame.image.save(ftext, "E:/open.jpg")#图片保存地址
注意:如果要生成中文其中的字体设置中,要设置支持中文的字体。不然会生成乱码
来源: http://www.phpxs.com/code/1005143/