26 lines
820 B
Python
26 lines
820 B
Python
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.rotate(-90).show() |