Skip to content

Instantly share code, notes, and snippets.

@nimpy
Last active May 12, 2024 04:07
Show Gist options
  • Save nimpy/5b0085075a54ba2e94f2cfabf5a98a57 to your computer and use it in GitHub Desktop.
Save nimpy/5b0085075a54ba2e94f2cfabf5a98a57 to your computer and use it in GitHub Desktop.
Calculating peak signal-to-noise ratio (PSNR) between two images.
import numpy as np
def calculate_psnr(img1, img2, max_value=255):
""""Calculating peak signal-to-noise ratio (PSNR) between two images."""
mse = np.mean((np.array(img1, dtype=np.float32) - np.array(img2, dtype=np.float32)) ** 2)
if mse == 0:
return 100
return 20 * np.log10(max_value / (np.sqrt(mse)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment