Created
April 9, 2015 21:31
-
-
Save kashifpk/11b144599709ed51eed6 to your computer and use it in GitHub Desktop.
autobahn slideshow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Threaded pygame based image slideshow | |
""" | |
import argparse | |
# import os | |
# import stat | |
import sys | |
import time | |
from glob import glob | |
from threading import Thread | |
from os.path import splitext | |
from Queue import Empty, Queue #, Full | |
import pygame | |
from pygame.locals import QUIT, KEYDOWN, K_ESCAPE | |
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner | |
from twisted.internet.defer import inlineCallbacks | |
from autobahn.twisted.util import sleep | |
image_q = Queue() # list of images to show | |
class SlideController(ApplicationSession): | |
@inlineCallbacks | |
def onJoin(self, details): | |
print("session joined") | |
def display_image(data): | |
print("Display image event received, data: {}".format(data)) | |
#yield sleep(1) | |
# notify that image has been displayed | |
self.publish(u'image_displayed', [data]) | |
try: | |
yield self.subscribe(display_image, u'display_image') | |
print("subscribed to topic: display_image") | |
except Exception as e: | |
print("could not subscribe to topic: {0}".format(e)) | |
class SlideShow(Thread): | |
"Slideshow controller class" | |
def __init__(self, file_list, interval=2): | |
"file_list is queue of image files to display" | |
Thread.__init__(self) | |
self.file_q = file_list | |
self.interval = interval | |
pygame.init() | |
# Test for image support | |
if not pygame.image.get_extended(): | |
print("Your Pygame isn't built with extended image support.") | |
print("It's likely this isn't going to work.") | |
sys.exit(1) | |
if self.file_q.empty(): | |
print "Sorry. No images found. Exiting." | |
sys.exit(1) | |
modes = pygame.display.list_modes() | |
pygame.display.set_mode(max(modes)) | |
self.screen = pygame.display.get_surface() | |
pygame.display.set_caption("Slider") | |
pygame.display.toggle_fullscreen() | |
def display_image(self, image_file): | |
"Display given image" | |
try: | |
img = pygame.image.load(image_file) | |
img = img.convert() | |
# rescale the image to fit the current display | |
#img = pygame.transform.scale(img, max(modes)) | |
self.screen.blit(img, (0, 0)) | |
pygame.display.flip() | |
except pygame.error as err: | |
print("Failed to display %s: %s" % (image_file, err)) | |
def run(self): | |
"Start the slideshow" | |
while True: | |
try: | |
image_file = self.file_q.get_nowait() | |
except Empty: | |
print("Queue is empty, waiting for queue to fill up") | |
time.sleep(1) | |
self.display_image(image_file) | |
self.file_q.task_done() | |
self.file_q.put_nowait(image_file) | |
self.input(pygame.event.get()) | |
time.sleep(self.interval) | |
def input(self, events): | |
"""A function to handle keyboard/mouse/device input events. """ | |
for event in events: # Hit the ESC key to quit the slideshow. | |
if (event.type == QUIT or | |
(event.type == KEYDOWN and event.key == K_ESCAPE)): | |
pygame.quit() | |
sys.exit(0) | |
def get_image_files(folder): | |
"Returns all JPEG (.jpg, jpeg) images in the given folder" | |
image_list = [] | |
if not folder.endswith('/'): | |
folder += '/' | |
files = glob(folder + '*.*') | |
for filepath in files: | |
if splitext(filepath.lower())[-1] in ['.jpg', '.jpeg', '.png']: | |
image_list.append(filepath) | |
return image_list | |
def main(startdir="."): | |
file_list = get_image_files(startdir) | |
for filename in file_list: | |
image_q.put_nowait(filename) | |
slider = SlideShow(image_q) | |
slider.daemon = True | |
slider.start() | |
#slider.run() | |
runner = ApplicationRunner(url = u"ws://localhost:8080/ws", realm = u"realm1") | |
runner.run(SlideController) | |
slider.join() | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
description='Recursively loads images ' | |
'from a directory, then displays them in a Slidshow.' | |
) | |
parser.add_argument( | |
'path', | |
metavar='ImagePath', | |
type=str, | |
default='.', | |
nargs="?", | |
help='Path to a directory that contains images' | |
) | |
parser.add_argument( | |
'--waittime', | |
type=int, | |
dest='waittime', | |
action='store', | |
default=1, | |
help='Amount of time to wait before showing the next image.' | |
) | |
parser.add_argument( | |
'--title', | |
type=str, | |
dest='title', | |
action='store', | |
default="pgSlidShow | My Slideshow!", | |
help='Set the title for the display window.' | |
) | |
args = parser.parse_args() | |
waittime = args.waittime | |
title = args.title | |
main(startdir=args.path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment