Skip to content

Instantly share code, notes, and snippets.

@Prasad9
Last active June 3, 2021 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Prasad9/077050d9a63df17cb1eaf33df4158b19 to your computer and use it in GitHub Desktop.
Save Prasad9/077050d9a63df17cb1eaf33df4158b19 to your computer and use it in GitHub Desktop.
Add salt and pepper noise to images
def add_salt_pepper_noise(X_imgs):
# Need to produce a copy as to not modify the original image
X_imgs_copy = X_imgs.copy()
row, col, _ = X_imgs_copy[0].shape
salt_vs_pepper = 0.2
amount = 0.004
num_salt = np.ceil(amount * X_imgs_copy[0].size * salt_vs_pepper)
num_pepper = np.ceil(amount * X_imgs_copy[0].size * (1.0 - salt_vs_pepper))
for X_img in X_imgs_copy:
# Add Salt noise
coords = [np.random.randint(0, i - 1, int(num_salt)) for i in X_img.shape]
X_img[coords[0], coords[1], :] = 1
# Add Pepper noise
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in X_img.shape]
X_img[coords[0], coords[1], :] = 0
return X_imgs_copy
salt_pepper_noise_imgs = add_salt_pepper_noise(X_imgs)
@sidharthskumar
Copy link

AttributeError: 'str' object has no attribute 'copy'

@maroov
Copy link

maroov commented Feb 26, 2020

AttributeError: 'str' object has no attribute 'copy'

from copy import deepcopy

then you can do X_imgs_copy = deepcopy(X_imgs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment