Inkycal/inkycal/modules/inkycal_image.py

109 lines
2.8 KiB
Python
Raw Normal View History

2022-04-02 01:30:17 +02:00
#!python3
"""
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)
2022-04-02 01:30:17 +02:00
class Inkyimage(inkycal_module):
2022-04-02 01:30:17 +02:00
"""Displays an image from URL or local path
"""
name = "Inkycal Image - show an image from a URL or local path"
2022-04-02 01:30:17 +02:00
requires = {
2022-04-02 01:30:17 +02: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."
},
2022-04-02 01:30:17 +02:00
"palette": {
"label": "Which palette should be used for converting images?",
"options": ["bw", "bwr", "bwy"]
}
2020-11-24 22:07:04 +01:00
}
2022-04-02 01:30:17 +02:00
optional = {
"autoflip": {
"label": "Should the image be flipped automatically?",
"options": [True, False]
},
2022-04-02 01:30:17 +02:00
"orientation": {
"label": "Please select the desired orientation",
"options": ["vertical", "horizontal"]
}
}
2020-11-24 00:40:49 +01:00
2022-04-02 01:30:17 +02:00
def __init__(self, config):
"""Initialize module"""
2022-04-02 01:30:17 +02:00
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:
if not param in config:
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
# give an OK message
print(f'{filename} 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
2022-04-02 01:30:17 +02:00
logger.info(f'Image size: {im_size}')
2022-04-02 01:30:17 +02:00
# initialize custom image class
im = Images()
2022-04-02 01:30:17 +02:00
# use the image at the first index
im.load(self.path)
2022-04-02 01:30:17 +02:00
# Remove background if present
im.remove_alpha()
2022-04-02 01:30:17 +02:00
# if autoflip 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
im_black, im_colour = im.to_palette(self.palette)
2022-04-02 01:30:17 +02:00
# with the images now send, clear the current image
im.clear()
2022-04-02 01:30:17 +02:00
# return images
return im_black, im_colour
if __name__ == '__main__':
2022-04-02 01:30:17 +02:00
print(f'running {filename} in standalone/debug mode')