diff --git a/inkycal/__init__.py b/inkycal/__init__.py index edbacf6..91d44d5 100644 --- a/inkycal/__init__.py +++ b/inkycal/__init__.py @@ -9,8 +9,9 @@ import inkycal.modules.inkycal_feeds import inkycal.modules.inkycal_todoist import inkycal.modules.inkycal_image import inkycal.modules.inkycal_jokes -import inkycal.modules.inkycal_stocks +import inkycal.modules.inkycal_slideshow # import inkycal.modules.inkycal_server # Main file -from inkycal.main import Inkycal \ No newline at end of file +from inkycal.main import Inkycal +import inkycal.modules.inkycal_stocks diff --git a/inkycal/modules/__init__.py b/inkycal/modules/__init__.py index 0a3921b..7ea2de5 100644 --- a/inkycal/modules/__init__.py +++ b/inkycal/modules/__init__.py @@ -5,5 +5,6 @@ from .inkycal_feeds import Feeds from .inkycal_todoist import Todoist from .inkycal_image import Inkyimage from .inkycal_jokes import Jokes +from .inkycal_stocks import Stocks +from .inkycal_slideshow import Slideshow #from .inkycal_server import Inkyserver -from .inkycal_stocks import Stocks \ No newline at end of file diff --git a/inkycal/modules/inkycal_slideshow.py b/inkycal/modules/inkycal_slideshow.py new file mode 100644 index 0000000..965025e --- /dev/null +++ b/inkycal/modules/inkycal_slideshow.py @@ -0,0 +1,129 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +""" +Image module for Inkycal Project +Copyright by aceisace +""" +import glob + +from inkycal.modules.template import inkycal_module +from inkycal.custom import * + +# PIL has a class named Image, use alias for Inkyimage -> Images +from inkycal.modules.inky_image import Inkyimage as Images + +filename = os.path.basename(__file__).split('.py')[0] +logger = logging.getLogger(filename) + +class Slideshow(inkycal_module): + """Cycles through images in a local image folder + """ + name = "Slideshow - cycle through images from a local folder" + + requires = { + + "path":{ + "label":"Path to a local folder, e.g. /home/pi/Desktop/images. " + "Only PNG and JPG/JPEG images are used for the slideshow." + }, + + "use_colour": { + "label":"Does the display support colour?", + "options": [True, False] + } + + } + + optional = { + + "autoflip":{ + "label":"Should the image be flipped automatically?", + "options": [True, False] + } + } + + def __init__(self, config): + """Initialize module""" + + super().__init__(config) + + config = config['config'] + + # required parameters + for param in self.requires: + if not param in config: + raise Exception(f'config is missing {param}') + + # optional parameters + self.path = config['path'] + self.use_colour = config['use_colour'] + self.autoflip = config['autoflip'] + + # Get the full path of all png/jpg/jpeg images in the given folder + all_files = glob.glob(f'{self.path}/*') + self.images = [i for i in all_files + if i.split('.')[-1].lower() in ('jpg', 'jpeg', 'png')] + + if not self.images: + logger.error('No images found in the given folder, please ' + 'double check your path!') + raise Exception('No images found in the given folder path :/') + + # set a 'first run' signal + self._first_run = True + + # give an OK message + print(f'{filename} loaded') + + def generate_image(self): + """Generate image for this module""" + + # Define new image size with respect to padding + im_width = int(self.width - (2 * self.padding_left)) + im_height = int(self.height - (2 * self.padding_top)) + im_size = im_width, im_height + + logger.info(f'Image size: {im_size}') + + # rotates list items by 1 index + def rotate(somelist): + return somelist[1:] + somelist[:1] + + # Switch to the next image if this is not the first run + if self._first_run == True: + self._first_run = False + else: + self.images = rotate(self.images) + + # initialize custom image class + im = Images() + + # use the image at the first index + im.load(self.images[0]) + + # Remove background if present + im.remove_alpha() + + # if autoflip was enabled, flip the image + if self.autoflip == True: + im.autoflip('vertical') + + # resize the image so it can fit on the epaper + im.resize( width=im_width, height=im_height ) + + # convert images according to given settings + if self.use_colour == False: + im_black = im.to_mono() + im_colour = Image.new('RGB', size = im_black.size, color = 'white') + else: + im_black, im_colour = im.to_colour() + + # with the images now send, clear the current image + im.clear() + + # return images + return im_black, im_colour + +if __name__ == '__main__': + print(f'running {filename} in standalone/debug mode')