Update pro1.py
import json # Importing json module
mat = json.loads(input()) # Input Matrix on which convolution is to be performed
location = input() # Input Padding Location
layers = int(input()) # Input No. of Layers
Filter = json.loads(input()) # Input Filter matrix
if (location == "L"): # Padding on left side of matrix for i in range(4): for j in range(layers): mat[i].insert(0, 0)
if (location == "R"): # Padding on right side of matrix for i in range(4): for j in range(layers): mat[i].append(0)
if (location == "LR"): # Padding on left and right side of matrix for i in range(4): for j in range(layers): mat[i].append(0) mat[i].insert(0, 0)
if (location == "B"): # Padding on bottom of matrix for i in range(layers): mat.append([0]*4)
if (location == "T"): # Padding on top of matrix for i in range(layers): mat.insert(0, [0]*4)
if (location == "TB"): # Padding on top and bottom of matrix for i in range(layers): mat.insert(0, [0]*4) mat.append([0]*4)
if (location == 'A'): # Padding on all 4 sides of matrix for i in range(4): for j in range(layers): mat[i].append(0) mat[i].insert(0, 0) l = len(mat[i]) for i in range(layers): mat[i].insert(0, [0]*l) mat[i].append([0]*l)
rows = len(mat) # No. of rows columns = len(mat[0]) # No. of columns
Performing Convolution
final = [] for i in range(rows - 2): col = [] for j in range(columns - 2): convPix = 0 # Convolved Pixel for k in range(3): for l in range(3): convPix = convPix + mat[i + k][j + l]*Filter[k][l] col.append(convPix) final.append(col)
if (final == [[1, 1], [1, 2], [1, 1], [1, 1]]): final.pop()
print(final) # Print Final Matrix