随处可见把大段的文字转成图片发布的。一直以为需要PIL才能实现,今天看到pygame也有这种功能,而且还很简单,看例子:
- # 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')