Commit dd4f9500 authored by Frank Scholz's avatar Frank Scholz
Browse files

a version that loads the image via a temporary file, for systems without PIL

parent 1710be2d
#!/usr/bin/python
# -*- coding: utf-8 -*-
import StringIO
import os
import random
# Twisted
......@@ -9,9 +9,6 @@ from twisted.internet import reactor
from twisted.internet import task
from twisted.web import client
# PIL
import Image
# Pygame
import pygame
from pygame.locals import *
......@@ -30,18 +27,23 @@ class MainBackground(object):
def __init__(self, screen, screenrect):
bgimagelocation="/home/dev/bg.png"
bgimagelocation='data/bg.png'
if os.path.exists(bgimagelocation):
self.image = pygame.image.load(bgimagelocation).convert()
self.image.set_colorkey((255,0,0), pygame.RLEACCEL)
else:
self.image = None
self.screen = screen
self.image = pygame.image.load(bgimagelocation).convert()
self.image.set_colorkey((255,0,0), pygame.RLEACCEL)
self.background = pygame.Surface(screenrect.size).convert()
self.makebg()
def makebg(self):
self.background.blit(self.image, (0, 0))
if self.image is not None:
self.background.blit(self.image, (0, 0))
else:
self.background.fill((0, 0, 0)) # black
def clearScreen(self):
self.screen.blit(self.background, (0, 0))
......@@ -96,26 +98,38 @@ class MediaRenderer(object):
d.addErrback(self.got_error, image_url)
def got_image(self, result, title = ''):
im = Image.open(StringIO.StringIO(result))
#im.convert("RGB")
flickr_image = pygame.image.frombuffer( im.tostring("raw","RGB"), im.size, "RGB")
try:
import Image
import StringIO
im = Image.open(StringIO.StringIO(result))
size = im.size
flickr_image = pygame.image.frombuffer( im.tostring("raw","RGB"), im.size, "RGB")
except ImportError:
import tempfile
tmp_fp, tmp_path = tempfile.mkstemp()
os.write(tmp_fp,result)
os.close(tmp_fp)
flickr_image = pygame.image.load(tmp_path).convert()
size = flickr_image.get_size()
os.unlink(tmp_path)
if self.displaying is not None:
self.mainbg.clear_part(self.displaying)
transform = None
if im.size[0] > self.screen.get_width():
if size[0] > self.screen.get_width():
new_width = self.screen.get_width()
relation = float(self.screen.get_width())/float(im.size[0])
new_heigth = float(im.size[1]) * relation
relation = float(self.screen.get_width())/float(size[0])
new_heigth = float(size[1]) * relation
new_height = int(new_height)
if new_height > self.screen.get_height():
relation = float(self.screen.get_height())/float(new_height)
new_width = float(new_width) * relation
new_height = self.screen.get_height()
transform =(new_width,new_height)
elif im.size[1] > self.screen.get_height():
relation = float(self.screen.get_height())/float(im.size[1])
elif size[1] > self.screen.get_height():
relation = float(self.screen.get_height())/float(size[1])
new_height = self.screen.get_height()
new_width = int(float(im.size[0]) * relation)
new_width = int(float(size[0]) * relation)
transform =(new_width,new_height)
if transform is not None:
flickr_image = pygame.transform.scale(flickr_image, transform)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment