import cv2 as cv
import sys
import numpy as np
import random as r
import os 
from PIL import Image as im

def noisy(noise_typ,image):
    if noise_typ == "gauss":
        # Generate Gaussian noise
        gauss = np.random.normal(0,1,image.size)
        print(gauss)
        gauss = gauss.reshape(image.shape[0],image.shape[1],image.shape[2]).astype('uint8')
        # Add the Gaussian noise to the image
        img_gauss = cv.add(image,gauss)
        cv.imwrite("Noise.png", gauss)
        return img_gauss

    elif noise_typ == "s&p":
      row,col,ch = image.shape
      s_vs_p = 0.5
      amount = 0.004
      out = np.copy(image)
      # Salt mode
      num_salt = np.ceil(amount * image.size * s_vs_p)
      coords = [np.random.randint(0, i - 1, int(num_salt))
              for i in image.shape]
      out[coords] = 1

      # Pepper mode
      num_pepper = np.ceil(amount* image.size * (1. - s_vs_p))
      coords = [np.random.randint(0, i - 1, int(num_pepper))
              for i in image.shape]
      out[coords] = 0
      return out
    elif noise_typ == "poisson":
        vals = len(np.unique(image))
        vals = 2 ** np.ceil(np.log2(vals))
        noisy = np.random.poisson(image * vals) / float(vals)
        return noisy
    elif noise_typ =="speckle":
        row,col,ch = image.shape
        gauss = np.random.randn(row,col,ch)
        gauss = gauss.reshape(row,col,ch)        
        noisy = image + image * gauss
        return noisy

img = cv.imread(cv.samples.findFile("3.png"))
if img is None:
    sys.exit("Could not read the image.")

else :
    width , height , depth = img.shape
    img_noisy = noisy("gauss",img)

    for kernal_size in range (1,71,2):
        print(kernal_size)
        dst = cv.GaussianBlur(img_noisy,(kernal_size,kernal_size),0)
        # print( cv.getGaussianKernel(kernal_size,0))
        file_name = "gaussian_blur" + str(kernal_size) + ".png"
        cv.imwrite(file_name, dst)

    #     dst = img_noisy
    # for kernal_no in range (0,200):
    #     print(kernal_no)
    #     dst = cv.GaussianBlur(dst,(3,3),1)
    #     # print( cv.getGaussianKernel(kernal_size,3))
    #     file_name = "gaussian_blur" + str(kernal_no) + ".png"
    #     cv.imwrite(file_name, dst)

    for kernal_size in range (1,71,2):
        print(kernal_size)
        dst = cv.bilateralFilter(img_noisy,kernal_size,300,300)
        # print( cv.getGaussianKernel(kernal_size,0))
        file_name = "bilateral_blur" + str(kernal_size) + ".png"
        cv.imwrite(file_name, dst)

[((48, 16, 48, 44), 'cv2.samples.findFile', 'cv.samples.findFile', ({(48, 36, 48, 43): '"""3.png"""'}, {}), "('3.png')", True, 'import cv2 as cv\n'), ((50, 4, 50, 41), 'sys.exit', 'sys.exit', ({(50, 13, 50, 40): '"""Could not read the image."""'}, {}), "('Could not read the image.')", False, 'import sys\n'), ((11, 16, 11, 48), 'numpy.random.normal', 'np.random.normal', ({(11, 33, 11, 34): '0', (11, 35, 11, 36): '1', (11, 37, 11, 47): 'image.size'}, {}), '(0, 1, image.size)', True, 'import numpy as np\n'), ((15, 20, 15, 39), 'cv2.add', 'cv.add', ({(15, 27, 15, 32): 'image', (15, 33, 15, 38): 'gauss'}, {}), '(image, gauss)', True, 'import cv2 as cv\n'), ((16, 8, 16, 38), 'cv2.imwrite', 'cv.imwrite', ({(16, 19, 16, 30): '"""Noise.png"""', (16, 32, 16, 37): 'gauss'}, {}), "('Noise.png', gauss)", True, 'import cv2 as cv\n'), ((58, 14, 58, 68), 'cv2.GaussianBlur', 'cv.GaussianBlur', ({(58, 30, 58, 39): 'img_noisy', (58, 40, 58, 65): '(kernal_size, kernal_size)', (58, 66, 58, 67): '0'}, {}), '(img_noisy, (kernal_size, kernal_size), 0)', True, 'import cv2 as cv\n'), ((61, 8, 61, 34), 'cv2.imwrite', 'cv.imwrite', ({(61, 19, 61, 28): 'file_name', (61, 30, 61, 33): 'dst'}, {}), '(file_name, dst)', True, 'import cv2 as cv\n'), ((73, 14, 73, 63), 'cv2.bilateralFilter', 'cv.bilateralFilter', ({(73, 33, 73, 42): 'img_noisy', (73, 43, 73, 54): 'kernal_size', (73, 55, 73, 58): '300', (73, 59, 73, 62): '300'}, {}), '(img_noisy, kernal_size, 300, 300)', True, 'import cv2 as cv\n'), ((76, 8, 76, 34), 'cv2.imwrite', 'cv.imwrite', ({(76, 19, 76, 28): 'file_name', (76, 30, 76, 33): 'dst'}, {}), '(file_name, dst)', True, 'import cv2 as cv\n'), ((23, 12, 23, 26), 'numpy.copy', 'np.copy', ({(23, 20, 23, 25): 'image'}, {}), '(image)', True, 'import numpy as np\n'), ((25, 17, 25, 54), 'numpy.ceil', 'np.ceil', ({(25, 25, 25, 53): 'amount * image.size * s_vs_p'}, {}), '(amount * image.size * s_vs_p)', True, 'import numpy as np\n'), ((31, 19, 31, 62), 'numpy.ceil', 'np.ceil', ({(31, 27, 31, 61): 'amount * image.size * (1.0 - s_vs_p)'}, {}), '(amount * image.size * (1.0 - s_vs_p))', True, 'import numpy as np\n'), ((37, 19, 37, 35), 'numpy.unique', 'np.unique', ({(37, 29, 37, 34): 'image'}, {}), '(image)', True, 'import numpy as np\n'), ((39, 16, 39, 47), 'numpy.random.poisson', 'np.random.poisson', ({(39, 34, 39, 46): '(image * vals)'}, {}), '(image * vals)', True, 'import numpy as np\n'), ((43, 16, 43, 43), 'numpy.random.randn', 'np.random.randn', ({(43, 32, 43, 35): 'row', (43, 36, 43, 39): 'col', (43, 40, 43, 42): 'ch'}, {}), '(row, col, ch)', True, 'import numpy as np\n'), ((38, 28, 38, 41), 'numpy.log2', 'np.log2', ({(38, 36, 38, 40): 'vals'}, {}), '(vals)', True, 'import numpy as np\n')]