Commit 2a6215c2 authored by Frank Scholz's avatar Frank Scholz
Browse files

Initial import of an simple pygame based image slideshow.

[[
This repository was converted from Subversion to Git on 2013-08-10 by
Hartmut Goebel <h.goebel@crazy-compilers.com>. Here and elsewhere,
conversion notes are enclosed in double square brackets. The original
Coherence multi-project repository has carefully been split into
separate repositories for each sub-project.

Obvious garbage has been removed, esp. build-artefacts like .aux
files, .egg-info directories, .pyc files, tar files. Junk commits
generated by e.g. remote moves have been removed, commit references
have been mapped into a uniform VCS-independent syntax, and some
comments edited into summary-plus-continuation form.
]]
parents
#!/usr/bin/python
# -*- coding: utf-8 -*-
import StringIO
import random
# Twisted
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 *
# Coherence
from coherence.base import Coherence
import louie
SCREENRECT = Rect(0, 0, 800, 480)
SCREENDEPTH = 32
class MainBackground(object):
topx = 10
topy = 7
rect = Rect(10, 7 , 750, 450)
def __init__(self, screen, screenrect):
bgimagelocation="/home/dev/bg.png"
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))
def clearScreen(self):
self.screen.blit(self.background, (0, 0))
def clear_part(self, rect):
self.screen.blit(self.background, rect)
class MediaRenderer(object):
def __init__(self, fullscreen=1):
pygame.init()
bestdepth = pygame.display.mode_ok(SCREENRECT.size, fullscreen, SCREENDEPTH)
self.screen = pygame.display.set_mode(SCREENRECT.size, fullscreen, bestdepth)
#pygame.mouse.set_visible(False)
# decorate the game window
pygame.display.set_caption('Coherence MediaRenderer')
self.mainbg = MainBackground(self.screen, SCREENRECT)
self.mainbg.clearScreen()
config = {}
config['logmode'] = 'info'
config['controlpoint'] = 'yes'
louie.connect(self.media_server_found, 'Coherence.UPnP.ControlPoint.MediaServer.detected', louie.Any)
louie.connect(self.media_server_removed, 'Coherence.UPnP.ControlPoint.MediaServer.removed', louie.Any)
Coherence(config)
event_loop = task.LoopingCall(self.pygame_loop)
event_loop.start(0.2, now=True)
self.display_loop = task.LoopingCall(self.proceed_to_next_image)
self.images = []
self.container_watch = []
self.image_index = 0
self.displaying = None
self.client = {}
def proceed_to_next_image(self):
if len(self.images)>0:
try:
image_url, image_title = self.images[self.image_index]
except IndexError:
self.image_index = 0
image_url, image_title = self.images[self.image_index]
self.image_index += 1
d = client.getPage(image_url)
d.addCallback(self.got_image, image_title)
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")
if self.displaying is not None:
self.mainbg.clear_part(self.displaying)
transform = None
if im.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
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])
new_height = self.screen.get_height()
new_width = int(float(im.size[0]) * relation)
transform =(new_width,new_height)
if transform is not None:
flickr_image = pygame.transform.scale(flickr_image, transform)
self.displaying = flickr_image.get_rect(center=((self.screen.get_width()/2)-1,(self.screen.get_height()/2)-1))
self.screen.blit(flickr_image,self.displaying)
pygame.display.flip()
pygame.display.set_caption('Coherence MediaRenderer - %s' % title)
def got_error(self, failure, url):
print "got_error", failure, url
def process_media_server_browse( self, results, usn):
for k,v in results.iteritems():
#print k, v
if k == 'items':
for id, values in v.iteritems():
if values['upnp_class'].startswith('object.container'):
d = self.client[usn].content_directory.browse(id, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_media_server_browse, usn)
if values['upnp_class'].startswith('object.item.imageItem'):
#print values
if values.has_key('resources'):
if(values.has_key('parent_id') and
values['parent_id'] not in self.container_watch):
self.container_watch.append(values['parent_id'])
if len(values['resources']):
title = values.get('title', 'untitled').encode('ascii', 'ignore')
self.images.append((values['resources'].keys()[0], title))
random.shuffle(self.images)
if len(self.images):
if not self.display_loop.running:
self.display_loop.start(20, now=True)
def media_server_found(self, client, usn):
print "media_server_found", client.device.get_friendly_name()
if 'Flickr Images' == client.device.get_friendly_name():
self.client[usn] = client
#d = client.content_directory.browse(0, browse_flag='BrowseMetadata')
d = self.client[usn].content_directory.browse(0, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_media_server_browse, usn)
client.content_directory.subscribe_for_variable('ContainerUpdateIDs', self.state_variable_change)
client.content_directory.subscribe_for_variable('SystemUpdateID', self.state_variable_change)
def media_server_removed(self, usn):
print "media_server_removed", usn
if self.client.has_key(usn) == True:
if self.display_loop.running:
self.display_loop.stop()
del self.client[usn]
self.images = []
def state_variable_change( self, variable, usn):
print variable.name, 'changed from', variable.old_value, 'to', variable.value
if variable.old_value == '':
return
if variable.name == 'ContainerUpdateIDs':
changes = variable.value.split(',')
while len(changes) > 1:
container = changes.pop(0).strip()
update_id = changes.pop(0).strip()
if container in self.container_watch:
print "heureka, we have a change in ", container, ", container needs a reload"
if self.display_loop.running:
self.display_loop.stop()
self.images = []
d = self.client[usn].content_directory.browse(container, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_media_server_browse, usn)
def pygame_loop(self):
for event in pygame.event.get():
#print event
if((event.type == KEYDOWN and event.key == K_ESCAPE) or
event.type == QUIT):
reactor.stop()
def main():
MediaRenderer()
reactor.run()
if __name__ == '__main__':
main()
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