forked from byuccl/byuccl.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize_student_images.py
39 lines (30 loc) · 1.16 KB
/
resize_student_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
from pathlib import Path
from PIL import Image, ImageOps
ROOT_PATH = Path(__file__).resolve().parent
def main():
student_photos_path = ROOT_PATH / "images" / "students"
for student_photo_path in student_photos_path.iterdir():
img = Image.open(student_photo_path)
if max(img.size) <= 400:
continue
print(student_photo_path, "is", img.size, "resizing...")
# Remove all exif tags
# https://github.com/python-pillow/Pillow/issues/4346
exif = img.getexif()
for k in exif.keys():
if k == 0x0112:
continue
# For some reason certain keys show up in the keys() list, but then still throw a KeyError
try:
# If I don't set it to None first (or print it) the del fails for some reason.
exif[k] = None
del exif[k]
except KeyError:
pass
new_exif = exif.tobytes()
img.info["exif"] = new_exif
img.thumbnail((400, 400), Image.Resampling.LANCZOS)
img = ImageOps.exif_transpose(img)
img.save(student_photo_path)
if __name__ == "__main__":
main()