Skip to content

Instantly share code, notes, and snippets.

@nimpy
Created February 17, 2019 13:54
Show Gist options
  • Save nimpy/cdd686b2cb08e61c87703a78aaeee466 to your computer and use it in GitHub Desktop.
Save nimpy/cdd686b2cb08e61c87703a78aaeee466 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment