python将文本转化成gif图片阅读
时间:2014-05-12 14:44:34
收藏:0
阅读:492
这是python吧的一个帖子(http://tieba.baidu.com/p/3030737423),具体的就是,导入txt文档,然后就会生成像一个gif的界面,文字不断的更换,用这种方法看文档,如图:
代码:
# -*- coding: utf-8 -*
#-------------------------------------
import pygame
from pygame.locals import *
from sys import exit
import time
import codecs
#获得文字
def get_text():
with open(‘test.txt‘,‘r‘) as fp:
#print fp.read()
return fp.read()
if __name__==‘__main__‘:
text=get_text()
print type(text)
text = text.decode(‘gbk‘)
length=len(text) #字符串长度
#print text
print length
sum=0
#初始化pygame
pygame.init()
#创建一个窗口
screen=pygame.display.set_mode((500,500),0,32)
#设置标题
pygame.display.set_caption(‘loster v0.1‘)
#设置文字属性
my_font=pygame.font.SysFont(‘SimHei‘,64)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
for i in range(length):
print text[i]
text_surface=my_font.render(text[i],True,(0,0,0),(255,255,255))
#绘制文字
screen.blit(text_surface,(200,200))
#暂停1000ms
pygame.time.wait(1000)
#刷新
pygame.display.update()
注意:
1,我是在win7+python IDLE下运行的,我的IDLE编码方式为utf-8
2,现在只能识别汉字,和一个个的字母,还不能识别单词
源码地址:https://github.com/iloster/PythonScripts/blob/master/text2gif.py
评论(0)