-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
47 lines (41 loc) · 1.48 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
from PIL import Image
import os
def rotate_image(img: Image) -> Image:
rotation = img.getexif().get(0x0112)
if rotation == 1:
pass
elif rotation == 2:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
elif rotation == 3:
img = img.rotate(180, expand=True)
elif rotation == 4:
img = img.transpose(Image.FLIP_TOP_BOTTOM)
elif rotation == 5:
img = img.rotate(90, expand=True)
img = img.transpose(Image.FLIP_LEFT_RIGHT)
elif rotation == 6:
img = img.rotate(270, expand=True)
elif rotation == 7:
img = img.rotate(270, expand=True)
img = img.transpose(Image.FLIP_TOP_BOTTOM)
elif rotation == 8:
img = img.rotate(90, expand=True)
else:
print(f"Unknown rotation: {rotation}, not in https://exiftool.org/TagNames/EXIF.html")
return img
def load_images_from_folder(folder: str):
images = []
files = os.listdir(folder)
files.sort()
files = list(filter(lambda x: x.lower().endswith(('.jpg', '.jpeg')), files))
for filename in files:
img_path = os.path.join(folder, filename)
img = Image.open(img_path)
images.append((img, img_path))
print(f"Found {len(images)} images in the folder.")
return images
def check_image_folder(folder: str):
files = os.listdir(folder)
files = list(filter(lambda x: x.lower().endswith(('.jpg', '.jpeg')), files))
print(f"Found {len(files)} images in the folder.")
return len(files) > 0