Add preserve_tone option to autocontrast

This commit is contained in:
elejke 2021-03-21 21:15:13 +03:00
parent ddcc936643
commit c585e6ab6b
2 changed files with 8 additions and 2 deletions

View File

@ -29,6 +29,7 @@ def test_sanity():
ImageOps.autocontrast(hopper("L"), cutoff=(2, 10)) ImageOps.autocontrast(hopper("L"), cutoff=(2, 10))
ImageOps.autocontrast(hopper("L"), ignore=[0, 255]) ImageOps.autocontrast(hopper("L"), ignore=[0, 255])
ImageOps.autocontrast(hopper("L"), mask=hopper("L")) ImageOps.autocontrast(hopper("L"), mask=hopper("L"))
ImageOps.autocontrast(hopper("L"), preserve_tone=True)
ImageOps.colorize(hopper("L"), (0, 0, 0), (255, 255, 255)) ImageOps.colorize(hopper("L"), (0, 0, 0), (255, 255, 255))
ImageOps.colorize(hopper("L"), "black", "white") ImageOps.colorize(hopper("L"), "black", "white")

View File

@ -61,7 +61,7 @@ def _lut(image, lut):
# actions # actions
def autocontrast(image, cutoff=0, ignore=None, mask=None): def autocontrast(image, cutoff=0, ignore=None, mask=None, preserve_tone=False):
""" """
Maximize (normalize) image contrast. This function calculates a Maximize (normalize) image contrast. This function calculates a
histogram of the input image (or mask region), removes ``cutoff`` percent of the histogram of the input image (or mask region), removes ``cutoff`` percent of the
@ -77,9 +77,14 @@ def autocontrast(image, cutoff=0, ignore=None, mask=None):
:param mask: Histogram used in contrast operation is computed using pixels :param mask: Histogram used in contrast operation is computed using pixels
within the mask. If no mask is given the entire image is used within the mask. If no mask is given the entire image is used
for histogram computation. for histogram computation.
:param preserve_tone: Preserve image tone in Photoshop-like style autocontrast.
:return: An image. :return: An image.
""" """
histogram = image.histogram(mask) if preserve_tone:
histogram = image.convert("L").histogram(mask)
else:
histogram = image.histogram(mask)
lut = [] lut = []
for layer in range(0, len(histogram), 256): for layer in range(0, len(histogram), 256):
h = histogram[layer : layer + 256] h = histogram[layer : layer + 256]