Improved formatting
This commit is contained in:
		| @@ -1,5 +1,4 @@ | ||||
| #!/usr/bin/python3 | ||||
| # -*- coding: utf-8 -*- | ||||
| #!python3 | ||||
|  | ||||
| """ | ||||
| Image module for Inkycal Project | ||||
| @@ -16,119 +15,121 @@ 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" | ||||
|     """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." | ||||
|       }, | ||||
|     requires = { | ||||
|  | ||||
|     "palette": { | ||||
|       "label":"Which palette should be used for converting images?", | ||||
|       "options": ["bw", "bwr", "bwy"] | ||||
|       } | ||||
|  | ||||
|     } | ||||
|  | ||||
|   optional = { | ||||
|      | ||||
|     "autoflip":{ | ||||
|         "label":"Should the image be flipped automatically? Default is False", | ||||
|         "options": [False, True] | ||||
|         "path": { | ||||
|             "label": "Path to a local folder, e.g. /home/pi/Desktop/images. " | ||||
|                      "Only PNG and JPG/JPEG images are used for the slideshow." | ||||
|         }, | ||||
|  | ||||
|     "orientation":{ | ||||
|       "label": "Please select the desired orientation", | ||||
|       "options": ["vertical", "horizontal"] | ||||
|       } | ||||
|         "palette": { | ||||
|             "label": "Which palette should be used for converting images?", | ||||
|             "options": ["bw", "bwr", "bwy"] | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
|   def __init__(self, config): | ||||
|     """Initialize module""" | ||||
|     optional = { | ||||
|  | ||||
|     super().__init__(config) | ||||
|         "autoflip": { | ||||
|             "label": "Should the image be flipped automatically? Default is False", | ||||
|             "options": [False, True] | ||||
|         }, | ||||
|  | ||||
|     config = config['config'] | ||||
|         "orientation": { | ||||
|             "label": "Please select the desired orientation", | ||||
|             "options": ["vertical", "horizontal"] | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     # required parameters | ||||
|     for param in self.requires: | ||||
|       if not param in config: | ||||
|         raise Exception(f'config is missing {param}') | ||||
|     def __init__(self, config): | ||||
|         """Initialize module""" | ||||
|  | ||||
|     # optional parameters | ||||
|     self.path = config['path'] | ||||
|     self.palette = config['palette'] | ||||
|     self.autoflip = config['autoflip'] | ||||
|     self.orientation = config['orientation'] | ||||
|         super().__init__(config) | ||||
|  | ||||
|     # 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')] | ||||
|         config = config['config'] | ||||
|  | ||||
|     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 :/') | ||||
|         # required parameters | ||||
|         for param in self.requires: | ||||
|             if not param in config: | ||||
|                 raise Exception(f'config is missing {param}') | ||||
|  | ||||
|     # set a 'first run' signal | ||||
|     self._first_run = True | ||||
|         # optional parameters | ||||
|         self.path = config['path'] | ||||
|         self.palette = config['palette'] | ||||
|         self.autoflip = config['autoflip'] | ||||
|         self.orientation = config['orientation'] | ||||
|  | ||||
|     # give an OK message | ||||
|     print(f'{filename} loaded') | ||||
|         # 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')] | ||||
|  | ||||
|   def generate_image(self): | ||||
|     """Generate image for this module""" | ||||
|         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 :/') | ||||
|  | ||||
|     # 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 | ||||
|         # set a 'first run' signal | ||||
|         self._first_run = True | ||||
|  | ||||
|     logger.info(f'Image size: {im_size}') | ||||
|         # give an OK message | ||||
|         print(f'{filename} loaded') | ||||
|  | ||||
|     # rotates list items by 1 index | ||||
|     def rotate(somelist): | ||||
|       return somelist[1:] + somelist[:1] | ||||
|     def generate_image(self): | ||||
|         """Generate image for this module""" | ||||
|  | ||||
|     # 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) | ||||
|         # 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 | ||||
|  | ||||
|     # initialize custom image class | ||||
|     im = Images() | ||||
|         logger.info(f'Image size: {im_size}') | ||||
|  | ||||
|     # temporary print method, prints current filename | ||||
|     print(f'slideshow - current image name: {self.images[0].split("/")[-1]}') | ||||
|         # rotates list items by 1 index | ||||
|         def rotate(somelist): | ||||
|             return somelist[1:] + somelist[:1] | ||||
|  | ||||
|     # use the image at the first index | ||||
|     im.load(self.images[0]) | ||||
|         # 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) | ||||
|  | ||||
|     # Remove background if present | ||||
|     im.remove_alpha() | ||||
|         # initialize custom image class | ||||
|         im = Images() | ||||
|  | ||||
|     # if autoflip was enabled, flip the image | ||||
|     if self.autoflip == True: | ||||
|       im.autoflip(self.orientation) | ||||
|         # temporary print method, prints current filename | ||||
|         print(f'slideshow - current image name: {self.images[0].split("/")[-1]}') | ||||
|  | ||||
|     # resize the image so it can fit on the epaper | ||||
|     im.resize( width=im_width, height=im_height ) | ||||
|         # use the image at the first index | ||||
|         im.load(self.images[0]) | ||||
|  | ||||
|     # convert images according to specified palette | ||||
|     im_black, im_colour = im.to_palette(self.palette) | ||||
|         # Remove background if present | ||||
|         im.remove_alpha() | ||||
|  | ||||
|     # with the images now send, clear the current image | ||||
|     im.clear() | ||||
|         # if autoflip was enabled, flip the image | ||||
|         if self.autoflip == True: | ||||
|             im.autoflip(self.orientation) | ||||
|  | ||||
|         # resize the image so it can fit on the epaper | ||||
|         im.resize(width=im_width, height=im_height) | ||||
|  | ||||
|         # convert images according to specified palette | ||||
|         im_black, im_colour = im.to_palette(self.palette) | ||||
|  | ||||
|         # with the images now send, clear the current image | ||||
|         im.clear() | ||||
|  | ||||
|         # return images | ||||
|         return im_black, im_colour | ||||
|  | ||||
|     # return images | ||||
|     return im_black, im_colour | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|   print(f'running {filename} in standalone/debug mode') | ||||
|     print(f'running {filename} in standalone/debug mode') | ||||
|   | ||||
		Reference in New Issue
	
	Block a user