import numpy as np
import argparse
import matplotlib.pyplot as plt
import math
from PIL import Image

def convolution(image, kernel, average=False):
    image_row, image_col = image.shape
    kernel_row, kernel_col = kernel.shape
    output = np.zeros(image.shape)
    pad_height = int((kernel_row - 1) / 2)
    pad_width = int((kernel_col - 1) / 2)
    padded_image = np.zeros((image_row + (2 * pad_height), image_col + (2 * pad_width)))
    padded_image[pad_height:padded_image.shape[0] - pad_height, pad_width:padded_image.shape[1] - pad_width] = image

    for row in range(image_row):
        for col in range(image_col):
            output[row, col] = np.sum(kernel * padded_image[row:row + kernel_row, col:col + kernel_col])
            if average:
                output[row, col] /= kernel.shape[0] * kernel.shape[1]
    return output


def mean_kernel(size, sigma=1):
    kernel_1D = np.linspace(-(size // 2), size // 2, size)
    for i in range(size):
        kernel_1D[i] = 1
    kernel_2D = np.outer(kernel_1D.T, kernel_1D.T)
    return kernel_2D


def mean_blur(image, kernel_size):
    kernel = mean_kernel(kernel_size, sigma=math.sqrt(kernel_size))
    return convolution(image, kernel, average=True)


def main():
    img = Image.open("testimg.bmp").convert("L")
    arr = np.array(img)
    ks = 9
    blrimg = mean_blur(arr, 9)
    img = Image.fromarray(blrimg)
    img.show()
    img = img.convert("L")
    img.save("result.png")

main()
