Inkycal/inkycal/modules/inkycal_image.py
Ace 3d4e24eee1 Adapted Inkycal_image
By using a helper class, this module could be simplified greatly
2020-11-29 23:45:17 +01:00

113 lines
2.7 KiB
Python

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Image module for Inkycal Project
Copyright by aceisace
"""
from inkycal.modules.template import inkycal_module
from inkycal.custom import *
from inkycal.modules.inky_image import Inkyimage as Images
filename = os.path.basename(__file__).split('.py')[0]
logger = logging.getLogger(filename)
class Inkyimage(inkycal_module):
"""Displays an image from URL or local path
"""
name = "Inykcal Image - show an image from a URL or local path"
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]
},
"orientation":{
"label": "Please select the desired orientation",
"options": ["vertical", "horizontal"]
}
}
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']
self.orientation = config['orientation']
# 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}')
# initialize custom image class
im = Images()
# use the image at the first index
im.load(self.path)
# Remove background if present
im.remove_alpha()
# 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 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')