This repository has been archived by the owner on Nov 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
turtle_images.py
58 lines (45 loc) · 1.53 KB
/
turtle_images.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
from PIL import Image, ExifTags
MAX_RES = 2048
ALPHA_BACKGROUND_COLOR = (255, 255, 255)
def size_to_quality(size):
if size <= 64:
return 98
elif size <= 256:
return 95
elif size <= 512:
return 90
elif size <= 1024:
return 75
else:
return 50
def exif_rotate(image):
try:
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == "Orientation":
break
exif = dict(image._getexif().items())
if exif[orientation] == 3:
image = image.rotate(180, expand=True)
elif exif[orientation] == 6:
image = image.rotate(270, expand=True)
elif exif[orientation] == 8:
image = image.rotate(90, expand=True)
return image
except (AttributeError, KeyError, IndexError):
return image
def prep_and_save(img_bytes, filename):
img: Image = Image.open(img_bytes)
# Apply rotation/crop specified by EXIF data
img = exif_rotate(img)
# Resize image
if max(img.size) > MAX_RES:
img.thumbnail((MAX_RES, MAX_RES))
# Flatten alpha to black if necessary
if img.mode == "RGBA":
flat_img = Image.new("RGB", img.size, ALPHA_BACKGROUND_COLOR)
flat_img.paste(img, mask=img.split()[3]) # 3 is the alpha channel
else:
flat_img = img
# Optimize and save image at 50% jpeg quality
quality = max([size_to_quality(size) for size in flat_img.size])
flat_img.convert("RGB").save(filename, optimize=True, quality=quality)