| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  | #!/usr/bin/python3 | 
					
						
							|  |  |  | # -*- coding: utf-8 -*- | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | Image module for Inkycal Project | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  | Copyright by aceisace | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | from inkycal.modules.template import inkycal_module | 
					
						
							|  |  |  | from inkycal.custom import * | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  | from inkycal.modules.inky_image import Inkyimage as Images | 
					
						
							| 
									
										
										
										
											2020-05-21 01:00:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | filename = os.path.basename(__file__).split('.py')[0] | 
					
						
							|  |  |  | logger = logging.getLogger(filename) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Inkyimage(inkycal_module): | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |   """Displays an image from URL or local path
 | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  |   """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-06 23:26:17 +01:00
										 |  |  |   name = "Inkycal Image - show an image from a URL or local path" | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   requires = { | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |      | 
					
						
							|  |  |  |     "path":{ | 
					
						
							|  |  |  |       "label":"Path to a local folder, e.g. /home/pi/Desktop/images. " | 
					
						
							|  |  |  |               "Only PNG and JPG/JPEG images are used for the slideshow." | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-02 01:01:00 +01:00
										 |  |  |     "palette": { | 
					
						
							|  |  |  |       "label":"Which palette should be used for converting images?", | 
					
						
							|  |  |  |       "options": ["bw", "bwr", "bwy"] | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-11-24 22:07:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |   optional = { | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     "autoflip":{ | 
					
						
							|  |  |  |         "label":"Should the image be flipped automatically?", | 
					
						
							|  |  |  |         "options": [True, False] | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     "orientation":{ | 
					
						
							|  |  |  |       "label": "Please select the desired orientation", | 
					
						
							|  |  |  |       "options": ["vertical", "horizontal"] | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-24 00:40:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  |   def __init__(self, config): | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     """Initialize module""" | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     super().__init__(config) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     config = config['config'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # required parameters | 
					
						
							|  |  |  |     for param in self.requires: | 
					
						
							|  |  |  |       if not param in config: | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |         raise Exception(f'config is missing {param}') | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # optional parameters | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     self.path = config['path'] | 
					
						
							| 
									
										
										
										
											2020-12-02 01:01:00 +01:00
										 |  |  |     self.palette = config['palette'] | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     self.autoflip = config['autoflip'] | 
					
						
							|  |  |  |     self.orientation = config['orientation'] | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # give an OK message | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     print(f'{filename} loaded') | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def generate_image(self): | 
					
						
							|  |  |  |     """Generate image for this module""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Define new image size with respect to padding | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     im_width = int(self.width - (2 * self.padding_left)) | 
					
						
							|  |  |  |     im_height = int(self.height - (2 * self.padding_top)) | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  |     im_size = im_width, im_height | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     logger.info(f'Image size: {im_size}') | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     # initialize custom image class | 
					
						
							|  |  |  |     im = Images() | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     # use the image at the first index | 
					
						
							|  |  |  |     im.load(self.path) | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     # Remove background if present | 
					
						
							|  |  |  |     im.remove_alpha() | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     # if autoflip was enabled, flip the image | 
					
						
							|  |  |  |     if self.autoflip == True: | 
					
						
							|  |  |  |       im.autoflip(self.orientation) | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     # resize the image so it can fit on the epaper | 
					
						
							|  |  |  |     im.resize( width=im_width, height=im_height ) | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-02 01:01:00 +01:00
										 |  |  |     # convert images according to specified palette | 
					
						
							|  |  |  |     im_black, im_colour = im.to_palette(self.palette) | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     # with the images now send, clear the current image | 
					
						
							|  |  |  |     im.clear() | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |     # return images | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  |     return im_black, im_colour | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2020-11-29 23:45:17 +01:00
										 |  |  |   print(f'running {filename} in standalone/debug mode') |