# Image convolution - image darkens

I'm using kernel convolution in python to blur an image, but in addition to blurring it also turns the image darker. Could someone explain why it happens?

def convolve(pixel, kernel, i, j):
R = 0
G = 0
B = 0
if i<2 or j<2 or i>=height-2 or j>=width-2:
R = pixel[i][j][0]
G = pixel[i][j][1]
B = pixel[i][j][2]
else:
# pixel[][][R/G/B]
R = (
pixel[i-2][j-2][0] * kernel[0][0] + pixel[i-2][j-1][0] * kernel[0][1] + pixel[i-2][j][0] * kernel[0][2] + pixel[i-2][j+1][0] * kernel[0][3] + pixel[i-2][j+2][0] * kernel[0][4] +
pixel[i-1  ][j-2][0] * kernel[1][0] + pixel[i-1  ][j-1  ][0] * kernel[1][1] +pixel[i-1  ][j][0] * kernel[1][2] + pixel[i-1  ][j+1][0] * kernel[1][3] + pixel[i-1  ][j+2][0] * kernel[1][4] +
pixel[i][j-2][0] * kernel[2][0] + pixel[i][j-1][0] * kernel[2][1] + pixel[i][j][0] * kernel[2][2] + pixel[i][j+1][0] * kernel[2][3] + pixel[i][j+2][0] * kernel[2][4]
)

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

B = (
pixel[i-2][j-2][2] * kernel[0][0] + pixel[i-2][j-1][2] * kernel[0][1] + pixel[i-2][j][2] * kernel[0][2] + pixel[i-2][j+1][2] * kernel[0][3] + pixel[i-2][j+2][2] * kernel[0][4] +
pixel[i-1  ][j-2][2] * kernel[1][0] + pixel[i-1  ][j-1][2] * kernel[1][1] +pixel[i-1][j][2] * kernel[1][2] + pixel[i-1][j+1][2] * kernel[1][3] + pixel[i-1  ][j+2][2] * kernel[1][4] +
pixel[i][j-2][2] * kernel[2][0] + pixel[i][j-1][2] * kernel[2][1] + pixel[i][j][2] * kernel[2][2] + pixel[i][j+1][2] * kernel[2][3] + pixel[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 else 255
G = G if G<256 else 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:
print(R, G, B)
return (round(R),round(G),round(B))


The kernel I'm using is a 5x5 gaussian blur from wikipedia

Simple modification is to switch RGB channels to color space using perceived luminance: $$Y = 0.299*R + 0.587*G + 0.114*B$$. That way, luminance and chrominance blurring will not result in darkening or artificial color bleeding.