1
0

ImageUtils.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from datetime import datetime
  2. from PIL import Image
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. def get_image_date(img_path: str) -> datetime:
  6. """Returns the date from the image EXIF data.
  7. Args:
  8. img_path (str): path to image
  9. Returns:
  10. datetime: datetime extracted from EXIF data
  11. """
  12. img = Image.open(img_path)
  13. date_raw = img.getexif()[306]
  14. return datetime.strptime(date_raw, "%Y:%m:%d %H:%M:%S")
  15. def display_images(images: list, titles: list, colorbar=False, size=(8, 5), row_size=2, **imshowargs):
  16. """Displays the given images next to each other.
  17. Args:
  18. images (list of np.ndarray): list of image arrays
  19. titles (list of str): list of titles
  20. colorbar (bool, optional): Display colorbars. Defaults to False.
  21. size (tuple of ints, optional): plt size (width, height) per image. Defaults to (8, 5).
  22. """
  23. num_imgs = len(images)
  24. num_cols = row_size
  25. num_rows = (num_imgs - 1) // num_cols + 1
  26. plt.figure(figsize=(num_cols * size[0], num_rows * size[1]))
  27. for i, image, title in zip(range(num_imgs), images, titles):
  28. plt.subplot(num_rows, num_cols, i + 1)
  29. plt.imshow(image, **imshowargs)
  30. plt.title(title)
  31. if colorbar:
  32. plt.colorbar()
  33. plt.tight_layout()
  34. plt.show()
  35. def is_daytime(img, threshold=50) -> bool:
  36. return np.mean([abs(img[:,:,0] - img[:,:,1]), abs(img[:,:,1] - img[:,:,2]), abs(img[:,:,2] - img[:,:,0])]) > threshold