E-Paper-Calendar/Calendar/ImageDesign.py

87 lines
2.8 KiB
Python
Raw Permalink Normal View History

2019-07-12 13:46:50 +02:00
from DesignEntity import DesignEntity
from Assets import path as application_path
from PIL import Image, ExifTags
2019-07-13 08:05:35 +02:00
2019-07-12 13:46:50 +02:00
class ImageDesign (DesignEntity):
"""Creates a TableDesign filled with rss post
date and title"""
2019-07-13 08:05:35 +02:00
# fill: "none" : original size, "stretch" : strech to fill, "scale" : scale to fill, "border" : scale until one side touches border
def __init__(self, size, path, fill="none", color="RGBA", dither=None):
2019-07-12 13:46:50 +02:00
super(ImageDesign, self).__init__(size)
self.set_path(path)
self.fill = fill
self.color = color
self.dither = dither
2019-07-13 08:05:35 +02:00
def set_path(self, path):
2019-07-12 13:46:50 +02:00
path = path.replace('\\', '/')
if path[0] != '/' and ':' not in path[0:3]:
path = application_path + '/' + path
self.path = path
2019-07-13 08:05:35 +02:00
def __finish_image__(self):
2019-07-12 13:46:50 +02:00
img = Image.open(self.path)
img = img.convert(self.color, dither=self.dither)
img = self.__fix_orientation__(img)
img = self.__resize_image__(img)
pos = self.__get_centered_position__(img)
self.__init_image__("#00000000")
self.draw(img, pos)
def __resize_image__(self, img):
if self.fill is "none":
return img
if self.fill is "stretch":
img = img.resize(self.size, resample=Image.LANCZOS)
if self.fill is "scale":
size = self.size
img_proportions = img.width / img.height
if img_proportions < size[0] / size[1]:
size = (size[0], int(size[0] * (1 / img_proportions)))
else:
size = (int(size[1] * img_proportions), size[1])
img = img.resize(size, resample=Image.LANCZOS)
if self.fill is "border":
size = self.size
img_proportions = img.width / img.height
if img_proportions < size[0] / size[1]:
size = (int(size[1] * img_proportions), size[1])
else:
size = (size[0], int(size[0] * (1 / img_proportions)))
img = img.resize(size, resample=Image.LANCZOS)
return img
def __get_centered_position__(self, img):
screen_size = self.size
img_size = img.size
delta_size = [s - i for s, i in zip(screen_size, img_size)]
delta_center_pos = [s / 2 for s in delta_size]
return delta_center_pos
def __fix_orientation__(self, img):
if "parsed_exif" not in img.info.keys():
return img
for orientation in ExifTags.TAGS.keys():
2019-07-13 08:05:35 +02:00
if ExifTags.TAGS[orientation] == 'Orientation':
2019-07-12 13:46:50 +02:00
break
2019-07-13 08:05:35 +02:00
exif = img.info["parsed_exif"]
2019-07-12 13:46:50 +02:00
if exif[orientation] == 3:
img = img.rotate(180, expand=True)
elif exif[orientation] == 6:
img = img.rotate(270, expand=True)
elif exif[orientation] == 8:
img = img.rotate(90, expand=True)
2019-07-13 08:05:35 +02:00
return img