You might want to take a look at the implementations in scikit-image which are good examples:
scikit-image/simple_metrics.py at main · scikit-image/scikit-image (github.com)
scikit-image/_structural_similarity.py at main · scikit-image/scikit-image (github.com)