一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

pygame实现文本转图片程序代码

时间:2013-05-11 编辑:简简单单 来源:一聚教程网

文本转图片其实就像php中的验证码程序了,下面我来各位同学介绍介绍。

text2pic.py

 代码如下 复制代码

# coding: utf-8
 
import pygame
pygame.init()
 
# 所有可用系统字体
# print pygame.font.get_fonts()
 
# 字体名, 大小
font = pygame.font.SysFont('microsoftyahei', 16)
 
# 字儿们, 是否开反锯齿, 前景色, 背景色 
text1 = font.render(u'根据相关法律', True, (0,0,0), (255,255,255))
pygame.image.save(text1, 'text1.jpg')
 
# 带透明的话不设背景色
text2 = font.render(u'此页无法显示', True, (0,0,0))
pygame.image.save(text2, 'text2.png')
 
# 组合
w, h = text1.get_size()
surface = pygame.Surface((w, 2*h))
surface.fill((255, 255, 255)) # 因为默认背景黑
surface.blit(text1, (0, 0))
surface.blit(text2, (0, h))
pygame.image.save(surface, 'all.png')

热门栏目