这两天倒饬python里的图像处理,想把从接口返回的图片做成流媒体。不过这样做有点太依赖网速了。一张图片大小为 640x480,大小30K,一张张取回来再显示出来不流畅。不管了,先把代码写出来,能运行后再考虑优化。
#!usr/bin/python
# -*- coding:utf-8 -*-
# FileName: xxx.py
import urllib2
import StringIO
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480)) # initialize a screen with size 640 x 480
pygame.display.set_caption("screen title") # set the screen title
pygame.display.flip() # update to the screen
# the picture url
url = "http://tuibang.com/test.jpg" // 或者使用 http://tuibang.com/2009/08/american-street.html
这个页面中的图片。
while 1:
try:
stream = urllib2.urlopen(url, timeout = 11)
tmpFile = pygame.image.load(StringIO.StringIO(stream.read()))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(tmpFile, (0, 0))
pygame.display.update()
except:
print "an error occurred"
sys.exit()
就是这样了。记录一下。继续努力学习。