Skip to content

Instantly share code, notes, and snippets.

@BilHim
Created February 17, 2021 22:30
Show Gist options
  • Save BilHim/3feed75ab028859c2b99497c32cf5309 to your computer and use it in GitHub Desktop.
Save BilHim/3feed75ab028859c2b99497c32cf5309 to your computer and use it in GitHub Desktop.
# Load image pixels
data = np.array(image)
# Half kernel size
a = kernel_size // 2
for x in range(width):
for y in range(height):
# min and max are used for edge pixels
x_start = max(x - a, 0)
y_start = max(y - a, 0)
x_end = min(x + a + 1, width)
y_end = min(y + a + 1, height)
# Array with all pixels inside square
square = data[y_start:y_end, x_start:x_end]
# We take their average
r_, g_, b_ = square.mean(axis=(0,1))
new_pixel = (int(r_),int(g_), int(b_))
new_image.putpixel((x, y), new_pixel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment