WIP: Context-Adaptive Interpolator (CAI)

This commit is contained in:
2024-03-21 10:03:44 +01:00
parent ad961b75fc
commit fe51649878
2 changed files with 37 additions and 23 deletions

View File

@@ -1,9 +1,9 @@
from PIL import Image
from statistics import mean
from statistics import mean, median
# What about other color channels?
image = Image.open('9f04e2005fddb9d5512e2f42a3b826b019755717.jpg').convert('L')
newI = image.load()
r = image.load()
I = image.copy().load()
DEFAULT_COLOR = 255
# This threshold is debatable.
@@ -21,6 +21,17 @@ for m in range(1, image.size[0] - 1):
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))
newPixel = I[m, n] - mean(A)
elif abs(e - w) - abs(no - s) > THRESHOLD:
newPixel = I[m, n] - (s + no) / 2
elif abs(s - no) - abs(e - w) > THRESHOLD:
newPixel = I[m, n] - (e + w) / 2
elif abs(sw - ne) - abs(se - nw) > THRESHOLD:
newPixel = I[m, n] - (se + nw) / 2
elif abs(se - nw) - abs(sw - ne) > THRESHOLD:
newPixel = I[m, n] - (sw + ne) / 2
else:
newPixel = I[m, n] - median(A)
r[m, n] = round(newPixel)
image.rotate(-90).show()