def salt_and_pepper(img, p):
    if (np.random.sample() <= p):
        output = np.copy(np.array(img))
        nb_salt = np.ceil(((p * output.size) * 0.5))
        coords = [np.random.randint(0, (i - 1), int(nb_salt)) for i in output.shape]
        for i in coords:
            output[i] = 1
        nb_pepper = np.ceil(((p * output.size) * 0.5))
        coords = [np.random.randint(0, (i - 1), int(nb_pepper)) for i in output.shape]
        for i in coords:
            output[i] = 0
        return Image.fromarray(output)
    else:
        return img
def salt_and_pepper(img, p):
    if (np.random.sample() <= p):
        output = np.copy(np.array(img))
        nb_salt = np.ceil(((p * output.size) * 0.5))
        coords = [np.random.randint(0, (i - 1), int(nb_salt)) for i in output.shape]
        for i in coords:
            output[i] = 1
        nb_pepper = np.ceil(((p * output.size) * 0.5))
        coords = [np.random.randint(0, (i - 1), int(nb_pepper)) for i in output.shape]
        for i in coords:
            output[i] = 0
        return Image.fromarray(output)
    else:
        return img