Inkycal/inkycal/modules/inkycal_slideshow.py

137 lines
4.1 KiB
Python
Raw Normal View History

"""
2022-10-02 00:49:27 +02:00
Inkycal Slideshow Module
2023-06-03 16:16:07 +02:00
Copyright by aceinnolab
"""
import glob
from inkycal.custom import *
# PIL has a class named Image, use alias for Inkyimage -> Images
2024-02-10 22:43:57 +01:00
from inkycal.modules.inky_image import Inkyimage as Images, image_to_palette
2023-12-18 12:46:33 +01:00
from inkycal.modules.template import inkycal_module
2024-06-23 20:13:58 +02:00
from inkycal.utils import JSONCache
2022-10-03 02:56:04 +02:00
logger = logging.getLogger(__name__)
2022-04-02 01:30:17 +02:00
class Slideshow(inkycal_module):
2024-05-12 02:00:26 +02:00
"""Cycles through images in a local image folder"""
2022-04-02 01:30:17 +02:00
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."
},
"palette": {
"label": "Which palette should be used for converting images?",
"options": ["bw", "bwr", "bwy"]
}
}
2022-04-02 01:30:17 +02:00
optional = {
"autoflip": {
"label": "Should the image be flipped automatically? Default is False",
"options": [False, True]
2020-11-29 23:45:48 +01:00
},
2022-04-02 01:30:17 +02:00
"orientation": {
"label": "Please select the desired orientation",
"options": ["vertical", "horizontal"]
}
}
2022-04-02 01:30:17 +02:00
def __init__(self, config):
"""Initialize module"""
super().__init__(config)
2022-04-02 01:30:17 +02:00
config = config['config']
2022-04-02 01:30:17 +02:00
# required parameters
for param in self.requires:
2024-05-12 02:00:26 +02:00
if param not in config:
2022-04-02 01:30:17 +02:00
raise Exception(f'config is missing {param}')
2022-04-02 01:30:17 +02:00
# optional parameters
self.path = config['path']
self.palette = config['palette']
self.autoflip = config['autoflip']
self.orientation = config['orientation']
2022-04-02 01:30:17 +02:00
# Get the full path of all png/jpg/jpeg images in the given folder
all_files = glob.glob(f'{self.path}/*')
2024-05-12 02:00:26 +02:00
self.images = [i for i in all_files if i.split('.')[-1].lower() in ('jpg', 'jpeg', 'png')]
2022-04-02 01:30:17 +02:00
if not self.images:
2024-05-12 02:00:26 +02:00
logger.error('No images found in the given folder, please double check your path!')
2022-04-02 01:30:17 +02:00
raise Exception('No images found in the given folder path :/')
2024-05-12 02:00:26 +02:00
self.cache = JSONCache('inkycal_slideshow')
self.cache_data = self.cache.read()
2022-04-02 01:30:17 +02:00
# set a 'first run' signal
self._first_run = True
2022-04-02 01:30:17 +02:00
# give an OK message
2024-06-25 14:27:54 +02:00
logger.debug(f'{__name__} loaded')
2022-04-02 01:30:17 +02:00
def generate_image(self):
"""Generate image for this module"""
2022-04-02 01:30:17 +02:00
# 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
2024-06-25 14:22:38 +02:00
logger.debug(f'Image size: {im_size}')
2022-04-02 01:30:17 +02:00
# rotates list items by 1 index
2024-05-12 02:00:26 +02:00
def rotate(list: list):
return list[1:] + list[:1]
2022-04-02 01:30:17 +02:00
# Switch to the next image if this is not the first run
2022-04-10 06:35:08 +02:00
if self._first_run:
2022-04-02 01:30:17 +02:00
self._first_run = False
2024-05-12 02:00:26 +02:00
self.cache_data["current_index"] = 0
2022-04-02 01:30:17 +02:00
else:
self.images = rotate(self.images)
2024-05-12 02:00:26 +02:00
self.cache_data["current_index"] = (self.cache_data["current_index"] + 1) % len(self.images)
2022-04-02 01:30:17 +02:00
# initialize custom image class
im = Images()
2022-04-02 01:30:17 +02:00
# temporary print method, prints current filename
print(f'slideshow - current image name: {self.images[0].split("/")[-1]}')
2022-04-02 01:30:17 +02:00
# use the image at the first index
im.load(self.images[0])
2020-12-02 01:01:00 +01:00
2022-04-02 01:30:17 +02:00
# Remove background if present
im.remove_alpha()
2024-05-12 02:00:26 +02:00
# if auto-flip was enabled, flip the image
2022-04-10 06:35:08 +02:00
if self.autoflip:
2022-04-02 01:30:17 +02:00
im.autoflip(self.orientation)
2022-04-02 01:30:17 +02:00
# resize the image so it can fit on the epaper
im.resize(width=im_width, height=im_height)
2022-04-02 01:30:17 +02:00
# convert images according to specified palette
2024-02-10 22:43:57 +01:00
im_black, im_colour = image_to_palette(im.image.convert("RGB"), self.palette)
2022-04-02 01:30:17 +02:00
# with the images now send, clear the current image
im.clear()
2024-05-12 02:00:26 +02:00
self.cache.write(self.cache_data)
2022-04-02 01:30:17 +02:00
# return images
return im_black, im_colour
if __name__ == '__main__':
2022-10-03 02:56:04 +02:00
print(f'running {__name__} in standalone/debug mode')