Skip to content

Instantly share code, notes, and snippets.

View nimpy's full-sized avatar
💭
Akku fast leer

Nina nimpy

💭
Akku fast leer
View GitHub Profile
@nimpy
nimpy / calculate_psnr.py
Last active May 1, 2025 16:52
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)))
@nimpy
nimpy / add_gaussian_noise.py
Created February 17, 2019 13:54
Add Gaussian noise to an image of type np.uint8.
import numpy as np
def add_gaussian_noise(image, mean=0, sigma=20):
"""Add Gaussian noise to an image of type np.uint8."""
gaussian_noise = np.random.normal(mean, sigma, image.shape)
gaussian_noise = gaussian_noise.reshape(image.shape)
noisy_image = image + gaussian_noise
noisy_image = np.clip(noisy_image, 0, 255)
noisy_image = noisy_image.astype(np.uint8)
return noisy_image
@nimpy
nimpy / calculate_ssd.py
Last active November 4, 2022 08:29
Computing the sum of squared differences (SSD) between two images.
import numpy as np
def calculate_ssd(img1, img2):
"""Computing the sum of squared differences (SSD) between two images."""
if img1.shape != img2.shape:
print("Images don't have the same shape.")
return
return np.sum((np.array(img1, dtype=np.float32) - np.array(img2, dtype=np.float32))**2)