-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
62 lines (55 loc) · 2.29 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
# in compliance with the License. A copy of the License is located at
#
# https://aws.amazon.com/apache-2-0/
#
# or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
"""Shared utility helper functions"""
import os
from io import BytesIO
from PIL import Image, ExifTags
EXIF_ORIENTATION = 274 # Magic numbers from http://www.exiv2.org/tags.html
def random_hex_bytes(n_bytes):
"""Create a hex encoded string of random bytes"""
return os.urandom(n_bytes).hex()
def resize_image(file_p, size):
"""Resize an image to fit within the size, and save to the path directory"""
dest_ratio = size[0] / float(size[1])
try:
image = Image.open(file_p)
except IOError:
print("Error: Unable to open image")
return None
try:
exif = dict(image._getexif().items())
if exif[EXIF_ORIENTATION] == 3:
image = image.rotate(180, expand=True)
elif exif[EXIF_ORIENTATION] == 6:
image = image.rotate(270, expand=True)
elif exif[EXIF_ORIENTATION] == 8:
image = image.rotate(90, expand=True)
except:
print("No exif data")
source_ratio = image.size[0] / float(image.size[1])
# the image is smaller than the destination on both axis
# don't scale it
if image.size < size:
new_width, new_height = image.size
elif dest_ratio > source_ratio:
new_width = int(image.size[0] * size[1]/float(image.size[1]))
new_height = size[1]
else:
new_width = size[0]
new_height = int(image.size[1] * size[0]/float(image.size[0]))
image = image.resize((new_width, new_height), resample=Image.LANCZOS)
final_image = Image.new("RGBA", size)
topleft = (int((size[0]-new_width) / float(2)),
int((size[1]-new_height) / float(2)))
final_image.paste(image, topleft)
bytes_stream = BytesIO()
final_image.save(bytes_stream, 'PNG')
return bytes_stream.getvalue()