From 955582e0f8bbd7932872e5950d6d2e8e65c61836 Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Tue, 19 Mar 2024 21:21:45 +0100 Subject: [PATCH] Add `algorithms/context-adaptive_interpolator.py` --- algorithms/context-adaptive_interpolator.py | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 algorithms/context-adaptive_interpolator.py diff --git a/algorithms/context-adaptive_interpolator.py b/algorithms/context-adaptive_interpolator.py new file mode 100644 index 0000000..d530224 --- /dev/null +++ b/algorithms/context-adaptive_interpolator.py @@ -0,0 +1,26 @@ +from PIL import Image +from statistics import mean + +# What about other color channels? +image = Image.open('9f04e2005fddb9d5512e2f42a3b826b019755717.jpg').convert('L') +newI = image.load() +I = image.copy().load() +DEFAULT_COLOR = 255 +# This threshold is debatable. +THRESHOLD = 20 +# How to manage the border of the image? +for m in range(1, image.size[0] - 1): + for n in range(1, image.size[1] - 1): + e = I[m, n + 1] + se = I[m + 1, n + 1] + s = I[m + 1, n] + sw = I[m + 1, n - 1] + w = I[m, n - 1] + nw = I[m - 1, n - 1] + no = I[m - 1, n] + ne = I[m - 1, n + 1] + A = [e, se, s, sw, w, nw, no, ne] + if max(A) - min(A) <= THRESHOLD: + newI[m, n] = int(round(I[m, n] - mean(A), 0)) + +image.show() \ No newline at end of file