# Matrices – Python Image Folding – image darkens

I use kernel convolution in Python to blur a picture, but in addition to blurring, it also gets darker. Could someone help me, why?

``````def convolve (pixels, kernels, i, j):
R = 0
G = 0
B = 0
if I have <2 or j<2 or i>= Height-2 or j> = width-2:
R = pixels[i][j][0]

G = pixel[i][j][1]

B = pixels[i][j][2]

otherwise:
# Pixels[][][R/G/B]

R = (
pixel[i-2][j-2][0]    * Kernel[0][0]    + Pixels[i-2][j-1][0]    * Kernel[0][1]    + Pixels[i-2][j][0]    * Kernel[0][2]    + Pixels[i-2][j+1][0]    * Kernel[0][3]    + Pixels[i-2][j+2][0]    * Kernel[0][4]    +
pixel[i-1  ][j-2][0]    * Kernel[1][0]    + Pixels[i-1  ][j-1  ][0]    * Kernel[1][1]    + Pixels[i-1  ][j][0]    * Kernel[1][2]    + Pixels[i-1  ][j+1][0]    * Kernel[1][3]    + Pixels[i-1  ][j+2][0]    * Kernel[1][4]    +
pixel[i][j-2][0]    * Kernel[2][0]    + Pixels[i][j-1][0]    * Kernel[2][1]    + Pixels[i][j][0]    * Kernel[2][2]    + Pixels[i][j+1][0]    * Kernel[2][3]    + Pixels[i][j+2][0]    * Kernel[2][4]

)

G = (
pixel[i-2][j-2][1]    * Kernel[0][0]    + Pixels[i-2][j-1][1]    * Kernel[0][1]    + Pixels[i-2][j][1]    * Kernel[0][2]    + Pixels[i-2][j+1][1]    * Kernel[0][3]    + Pixels[i-2][j+2][1]    * Kernel[0][4]    +
pixel[i-1  ][j-2][1]    * Kernel[1][0]    + Pixels[i-1  ][j-1  ][1]    * Kernel[1][1]    + Pixels[i-1  ][j][1]    * Kernel[1][2]    + Pixels[i-1][j+1][1]    * Kernel[1][3]    + Pixels[i-1  ][j+2][1]    * Kernel[1][4]    +
pixel[i][j-2][1]    * Kernel[2][0]    + Pixels[i][j-1][1]    * Kernel[2][1]    + Pixels[i][j][1]    * Kernel[2][2]    + Pixels[i][j+1][1]    * Kernel[2][3]    + Pixels[i][j+2][1]    * Kernel[2][4]

)

B = (
pixel[i-2][j-2][2]    * Kernel[0][0]    + Pixels[i-2][j-1][2]    * Kernel[0][1]    + Pixels[i-2][j][2]    * Kernel[0][2]    + Pixels[i-2][j+1][2]    * Kernel[0][3]    + Pixels[i-2][j+2][2]    * Kernel[0][4]    +
pixel[i-1  ][j-2][2]    * Kernel[1][0]    + Pixels[i-1  ][j-1][2]    * Kernel[1][1]    + Pixels[i-1][j][2]    * Kernel[1][2]    + Pixels[i-1][j+1][2]    * Kernel[1][3]    + Pixels[i-1  ][j+2][2]    * Kernel[1][4]    +
pixel[i][j-2][2]    * Kernel[2][0]    + Pixels[i][j-1][2]    * Kernel[2][1]    + Pixels[i][j][2]    * Kernel[2][2]    + Pixels[i][j+1][2]    * Kernel[2][3]    + Pixels[i][j+2][2]    * Kernel[2][4]

)

# if <0, then 0
R = R*(R>0)
G = G * (G> 0)
B = B * (B> 0)

R = R if R <256, otherwise 255
G = G if G <256, otherwise 255
B = B if B<256 else 255

# if >255, then 255
if R<0 or R>255 or G<0 or G>255 or B<0 or B>255:
Pressure (R, G, B)
Return (Round (R), Round (G), Round (B))
``````

The kernel I use is a 5×5 Gaussian Blur from Wikipedia