2020-05-21 01:00:37 +02:00
|
|
|
"""
|
2022-10-02 00:49:27 +02:00
|
|
|
Inkycal Image Module
|
2023-06-03 16:16:07 +02:00
|
|
|
Copyright by aceinnolab
|
2020-05-21 01:00:37 +02:00
|
|
|
"""
|
2020-11-10 22:48:04 +01:00
|
|
|
from inkycal.custom import *
|
2024-01-20 17:15:03 +01:00
|
|
|
from inkycal.modules.inky_image import image_to_palette
|
2020-11-29 23:45:17 +01:00
|
|
|
from inkycal.modules.inky_image import Inkyimage as Images
|
2023-12-18 12:46:33 +01:00
|
|
|
from inkycal.modules.template import inkycal_module
|
2020-05-21 01:00:37 +02:00
|
|
|
|
2022-10-03 02:56:04 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
|
2020-11-10 22:48:04 +01:00
|
|
|
class Inkyimage(inkycal_module):
|
2024-01-20 17:15:03 +01:00
|
|
|
"""Displays an image from URL or local path"""
|
2022-04-02 01:30:17 +02:00
|
|
|
|
|
|
|
name = "Inkycal Image - show an image from a URL or local path"
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
requires = {
|
|
|
|
"path": {
|
|
|
|
"label": "Path to a local folder, e.g. /home/pi/Desktop/images. "
|
2024-01-20 17:15:03 +01:00
|
|
|
"Only PNG and JPG/JPEG images are used for the slideshow."
|
2022-04-02 01:30:17 +02:00
|
|
|
},
|
2024-01-20 17:15:03 +01:00
|
|
|
"palette": {"label": "Which palette should be used for converting images?", "options": ["bw", "bwr", "bwy"]},
|
2020-11-10 22:48:04 +01:00
|
|
|
}
|
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
optional = {
|
2024-01-20 17:15:03 +01:00
|
|
|
"autoflip": {"label": "Should the image be flipped automatically?", "options": [True, False]},
|
|
|
|
"orientation": {"label": "Please select the desired orientation", "options": ["vertical", "horizontal"]},
|
2020-11-29 23:45:17 +01:00
|
|
|
}
|
2020-11-24 00:40:49 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize module"""
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
super().__init__(config)
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2024-01-20 17:15:03 +01:00
|
|
|
config = config["config"]
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
# required parameters
|
|
|
|
for param in self.requires:
|
|
|
|
if not param in config:
|
2024-01-20 17:15:03 +01:00
|
|
|
raise Exception(f"config is missing {param}")
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
# optional parameters
|
2024-01-20 17:15:03 +01:00
|
|
|
self.path = config["path"]
|
|
|
|
self.palette = config["palette"]
|
|
|
|
self.autoflip = config["autoflip"]
|
|
|
|
self.orientation = config["orientation"]
|
2023-12-13 13:14:59 +01:00
|
|
|
self.dither = True
|
2024-01-20 17:15:03 +01:00
|
|
|
if "dither" in config and config["dither"] == False:
|
2023-12-13 13:14:59 +01:00
|
|
|
self.dither = False
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
# give an OK message
|
2024-01-20 17:15:03 +01:00
|
|
|
print(f"{__name__} loaded")
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
def generate_image(self):
|
|
|
|
"""Generate image for this module"""
|
2020-11-10 22:48:04 +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-10 22:48:04 +01:00
|
|
|
|
2024-01-20 17:15:03 +01:00
|
|
|
logger.info(f"Image size: {im_size}")
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
# initialize custom image class
|
|
|
|
im = Images()
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
# use the image at the first index
|
|
|
|
im.load(self.path)
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
# Remove background if present
|
|
|
|
im.remove_alpha()
|
2020-11-10 22:48:04 +01:00
|
|
|
|
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)
|
2020-11-10 22:48:04 +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-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
# convert images according to specified palette
|
2024-02-10 22:43:57 +01:00
|
|
|
im_black, im_colour = image_to_palette(image=im.image.convert("RGB"), palette=self.palette, dither=self.dither)
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
# with the images now send, clear the current image
|
|
|
|
im.clear()
|
2020-11-10 22:48:04 +01:00
|
|
|
|
2022-04-02 01:30:17 +02:00
|
|
|
# return images
|
|
|
|
return im_black, im_colour
|
2020-11-10 22:48:04 +01:00
|
|
|
|
|
|
|
|
2024-01-20 17:15:03 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
print(f"running {__name__} in standalone/debug mode")
|