Skip to content

Commit

Permalink
New and improved resize images!
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 committed Jun 16, 2020
1 parent c623f00 commit 0bbbbf1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion custom_components/mail_and_packages/const.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 8 additions & 2 deletions custom_components/mail_and_packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
50 changes: 25 additions & 25 deletions custom_components/mail_and_packages/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0bbbbf1

Please sign in to comment.