Created
February 17, 2021 22:30
-
-
Save BilHim/3feed75ab028859c2b99497c32cf5309 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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