import matplotlib.pyplot as plt import numpy as np from scipy.misc import face def convolve(image, mask): ret = np.zeros_like(image).astype("f4") for y in range(1, image.shape[0]-1): for x in range(1, image.shape[1]-1): ret[y, x] = (image[y-1:y+2, x-1:x+2] * mask).sum() return ret[1:-1, 1:-1] image = face(gray=True) mask = [[-1, -1, -1], [2, 2, 2], [-1, -1, -1]] mask = [[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]] #mask = np.ones((3, 3)) / 9 # mask = np.zeros((3, 3)) result = convolve(image, mask) plt.subplot(121) plt.imshow(image) plt.subplot(122) plt.imshow(result) plt.show()