From 0bbbbf1a85e10053276939f2f488488d62f5439d Mon Sep 17 00:00:00 2001 From: firstof9 Date: Tue, 16 Jun 2020 15:08:22 -0700 Subject: [PATCH] New and improved resize images! --- custom_components/mail_and_packages/const.py | 2 +- .../mail_and_packages/manifest.json | 10 +++- custom_components/mail_and_packages/sensor.py | 50 +++++++++---------- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/custom_components/mail_and_packages/const.py b/custom_components/mail_and_packages/const.py index 39d43be2..1a076c83 100644 --- a/custom_components/mail_and_packages/const.py +++ b/custom_components/mail_and_packages/const.py @@ -1,5 +1,5 @@ DOMAIN = "mail_and_packages" -VERSION = "0.2.1-b7-1" +VERSION = "0.2.1-resize" ISSUE_URL = "http://github.com/moralmunky/Home-Assistant-Mail-And-Packages" # Configuration Properties diff --git a/custom_components/mail_and_packages/manifest.json b/custom_components/mail_and_packages/manifest.json index 92e448fc..60ce6ae6 100644 --- a/custom_components/mail_and_packages/manifest.json +++ b/custom_components/mail_and_packages/manifest.json @@ -3,7 +3,13 @@ "name": "Mail and Packages", "documentation": "https://github.com/moralmunky/Home-Assistant-Mail-And-Packages", "dependencies": [], - "codeowners": ["@moralmunky","@firstof9"], + "codeowners": [ + "@moralmunky", + "@firstof9" + ], "config_flow": true, - "requirements": [ "imageio" ] + "requirements": [ + "imageio", + "python-resize-image" + ] } \ No newline at end of file diff --git a/custom_components/mail_and_packages/sensor.py b/custom_components/mail_and_packages/sensor.py index 451daecb..ae84f521 100644 --- a/custom_components/mail_and_packages/sensor.py +++ b/custom_components/mail_and_packages/sensor.py @@ -15,6 +15,8 @@ import uuid from datetime import timedelta from shutil import copyfile +from PIL import Image +from resizeimage import resizeimage import quopri from homeassistant.helpers.entity import Entity @@ -444,11 +446,9 @@ def get_mails(account, image_output_path, gif_duration, image_name): if image_count > 0: all_images = [] - """ - # _LOGGER.debug("Resizing images to 700x315...") - # # Resize images to 700x315 - # all_images = resize_images(all_images) - """ + _LOGGER.debug("Resizing images to 724x320...") + """Resize images to 724x320""" + all_images = resize_images(images, 724, 320) """Create numpy array of images""" _LOGGER.debug("Creating array of image files...") @@ -492,30 +492,30 @@ def get_mails(account, image_output_path, gif_duration, image_name): return image_count -# Resize images -# This should keep the aspect ratio of the images -################################################# - - -# def resize_images(images): -# sized_images = [] -# for image in images: -# if image.shape[1] < 700: -# wpercent = 700/image.shape[1] -# height = int(float(image.shape[0])*float(wpercent)) -# sized_images.append(img_as_ubyte(resize(image, (height, 700)))) -# else: -# sized_images.append(img_as_ubyte(resize(image, (317, 700), -# mode='symmetric'))) -# return sized_images +def resize_images(images, width, height): + """ + Resize images + This should keep the aspect ratio of the images + """ + for image in images: + try: + fd_img = open(image, "rw") + except Exception as err: + _LOGGER.error("Error attempting to open image %s: %s", str(image), str(err)) + continue + img = Image.open(fd_img) + img = resizeimage.resize_contain(img, [width, height]) + img.save(image, img.format) + fd_img.close() -""" -Clean up image storage directory -Only supose to delete .gif files -""" + return images def cleanup_images(path): + """ + Clean up image storage directory + Only supose to delete .gif files + """ for file in os.listdir(path): if file.endswith(".gif"): os.remove(path + file)