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)

[((1478, 1506), 'cv2.samples.findFile', 'cv.samples.findFile', (['"""3.png"""'], {}), "('3.png')\n", (1497, 1506), True, 'import cv2 as cv\n'), ((1528, 1565), 'sys.exit', 'sys.exit', (['"""Could not read the image."""'], {}), "('Could not read the image.')\n", (1536, 1565), False, 'import sys\n'), ((213, 247), 'numpy.random.normal', 'np.random.normal', (['(0)', '(1)', 'image.size'], {}), '(0, 1, image.size)\n', (229, 247), True, 'import numpy as np\n'), ((425, 445), 'cv2.add', 'cv.add', (['image', 'gauss'], {}), '(image, gauss)\n', (431, 445), True, 'import cv2 as cv\n'), ((453, 483), 'cv2.imwrite', 'cv.imwrite', (['"""Noise.png"""', 'gauss'], {}), "('Noise.png', gauss)\n", (463, 483), True, 'import cv2 as cv\n'), ((1729, 1786), 'cv2.GaussianBlur', 'cv.GaussianBlur', (['img_noisy', '(kernal_size, kernal_size)', '(0)'], {}), '(img_noisy, (kernal_size, kernal_size), 0)\n', (1744, 1786), True, 'import cv2 as cv\n'), ((1910, 1936), 'cv2.imwrite', 'cv.imwrite', (['file_name', 'dst'], {}), '(file_name, dst)\n', (1920, 1936), True, 'import cv2 as cv\n'), ((2312, 2364), 'cv2.bilateralFilter', 'cv.bilateralFilter', (['img_noisy', 'kernal_size', '(300)', '(300)'], {}), '(img_noisy, kernal_size, 300, 300)\n', (2330, 2364), True, 'import cv2 as cv\n'), ((2489, 2515), 'cv2.imwrite', 'cv.imwrite', (['file_name', 'dst'], {}), '(file_name, dst)\n', (2499, 2515), True, 'import cv2 as cv\n'), ((622, 636), 'numpy.copy', 'np.copy', (['image'], {}), '(image)\n', (629, 636), True, 'import numpy as np\n'), ((672, 709), 'numpy.ceil', 'np.ceil', (['(amount * image.size * s_vs_p)'], {}), '(amount * image.size * s_vs_p)\n', (679, 709), True, 'import numpy as np\n'), ((867, 912), 'numpy.ceil', 'np.ceil', (['(amount * image.size * (1.0 - s_vs_p))'], {}), '(amount * image.size * (1.0 - s_vs_p))\n', (874, 912), True, 'import numpy as np\n'), ((1099, 1115), 'numpy.unique', 'np.unique', (['image'], {}), '(image)\n', (1108, 1115), True, 'import numpy as np\n'), ((1176, 1207), 'numpy.random.poisson', 'np.random.poisson', (['(image * vals)'], {}), '(image * vals)\n', (1193, 1207), True, 'import numpy as np\n'), ((1324, 1353), 'numpy.random.randn', 'np.random.randn', (['row', 'col', 'ch'], {}), '(row, col, ch)\n', (1339, 1353), True, 'import numpy as np\n'), ((1145, 1158), 'numpy.log2', 'np.log2', (['vals'], {}), '(vals)\n', (1152, 1158), True, 'import numpy as np\n')]