Files
Robust_image_source_identif…/datasets/rafael/240424/plot_dates.py

65 lines
1.9 KiB
Python

from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.dates as mdates
from PIL import Image
from PIL.ExifTags import TAGS
import os
path = 'photos'
os.chdir(path)
names = []
dates = []
for fileName in sorted(os.listdir()):
try:
image = Image.open(fileName)
except:
# Skip raw images.
continue
imageExif = image.getexif()
dateTimeKey = list(TAGS.keys())[list(TAGS.values()).index('DateTime')]
dateTime = imageExif[dateTimeKey]
names += [fileName.replace('DSC0', '').replace('.JPG', '')]
dates += [dateTime[:(-1 if fileName.endswith('.tif') else len(dateTime))]]
# Convert date strings to datetime
dates = [datetime.strptime(d, "%Y:%m:%d %H:%M:%S") for d in dates]
# Choose some nice levels
NUMBER_OF_LEVELS = 10
actualLevels = range(-NUMBER_OF_LEVELS * 2 + 1, NUMBER_OF_LEVELS * 2 , 2)
levels = np.tile(actualLevels,
int(np.ceil(len(dates)/len(actualLevels))))[:len(dates)]
# Create figure and plot a stem plot with the date
fig, ax = plt.subplots(figsize=(8.8, 4), layout="constrained")
#ax.set(title="Matplotlib release dates")
ax.vlines(dates, 0, levels, color="tab:red") # The vertical stems.
ax.plot(dates, np.zeros_like(dates), "-o",
color="k", markerfacecolor="w") # Baseline and markers on it.
# annotate lines
for d, l, r in zip(dates, levels, names):
ax.annotate(r, xy=(d, l),
xytext=(-3, np.sign(l)*3), textcoords="offset points",
horizontalalignment="right",
verticalalignment="bottom" if l > 0 else "top")
# format x-axis with 4-month intervals
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y:%m:%d %H:%M:%S"))
plt.setp(ax.get_xticklabels(), rotation=30, ha="right")
# remove y-axis and spines
ax.yaxis.set_visible(False)
ax.spines[["left", "top", "right"]].set_visible(False)
ax.margins(y=0.1)
plt.show()