ImageOps autocontrast cutoff updated

This commit is contained in:
Hamza 2020-07-01 00:48:38 +05:00
parent cc83723d6b
commit fb2a184eed

View File

@ -88,12 +88,18 @@ def autocontrast(image, cutoff=0, ignore=None):
h[ix] = 0 h[ix] = 0
if cutoff: if cutoff:
# cut off pixels from both ends of the histogram # cut off pixels from both ends of the histogram
if isinstance(cutoff, int):
cutoff = (cutoff, cutoff)
elif isinstance(cutoff, tuple):
pass
else:
raise ValueError("the cutoff can only be a integer or tuple")
# get number of pixels # get number of pixels
n = 0 n = 0
for ix in range(256): for ix in range(256):
n = n + h[ix] n = n + h[ix]
# remove cutoff% pixels from the low end # remove cutoff% pixels from the low end
cut = n * cutoff // 100 cut = n * cutoff[0] // 100
for lo in range(256): for lo in range(256):
if cut > h[lo]: if cut > h[lo]:
cut = cut - h[lo] cut = cut - h[lo]
@ -104,7 +110,7 @@ def autocontrast(image, cutoff=0, ignore=None):
if cut <= 0: if cut <= 0:
break break
# remove cutoff% samples from the hi end # remove cutoff% samples from the hi end
cut = n * cutoff // 100 cut = n * cutoff[1] // 100
for hi in range(255, -1, -1): for hi in range(255, -1, -1):
if cut > h[hi]: if cut > h[hi]:
cut = cut - h[hi] cut = cut - h[hi]