| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  | #!python3 | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | class Slideshow(inkycal_module): | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     """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." | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         "palette": { | 
					
						
							|  |  |  |             "label": "Which palette should be used for converting images?", | 
					
						
							|  |  |  |             "options": ["bw", "bwr", "bwy"] | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"] | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     def __init__(self, config): | 
					
						
							|  |  |  |         """Initialize module""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         super().__init__(config) | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         config = config['config'] | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # required parameters | 
					
						
							|  |  |  |         for param in self.requires: | 
					
						
							|  |  |  |             if not param in config: | 
					
						
							|  |  |  |                 raise Exception(f'config is missing {param}') | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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'] | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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}/*') | 
					
						
							|  |  |  |         self.images = [i for i in all_files | 
					
						
							|  |  |  |                        if i.split('.')[-1].lower() in ('jpg', 'jpeg', 'png')] | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         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 :/') | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # set a 'first run' signal | 
					
						
							|  |  |  |         self._first_run = True | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # give an OK message | 
					
						
							|  |  |  |         print(f'{filename} loaded') | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     def generate_image(self): | 
					
						
							|  |  |  |         """Generate image for this module""" | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         logger.info(f'Image size: {im_size}') | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # rotates list items by 1 index | 
					
						
							|  |  |  |         def rotate(somelist): | 
					
						
							|  |  |  |             return somelist[1:] + somelist[:1] | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # 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) | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # initialize custom image class | 
					
						
							|  |  |  |         im = Images() | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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]}') | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # if autoflip was enabled, flip the image | 
					
						
							|  |  |  |         if self.autoflip == True: | 
					
						
							|  |  |  |             im.autoflip(self.orientation) | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # convert images according to specified palette | 
					
						
							|  |  |  |         im_black, im_colour = im.to_palette(self.palette) | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # with the images now send, clear the current image | 
					
						
							|  |  |  |         im.clear() | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |         # return images | 
					
						
							|  |  |  |         return im_black, im_colour | 
					
						
							| 
									
										
										
										
											2020-11-29 14:52:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2022-04-02 01:30:17 +02:00
										 |  |  |     print(f'running {filename} in standalone/debug mode') |