Add algorithms/context-adaptive_interpolator.py

This commit is contained in:
Benjamin Loison 2024-03-19 21:21:45 +01:00
parent 681aff3a38
commit 955582e0f8
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8

View File

@ -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()