By adjusting the image hue value and combining with the ImageEnhance library to achieve hue enhancement

Foreword

The ImageEnhance class in the PIL library can be used for image enhancement, which can adjust the brightness, contrast, hue and sharpness of the image.

The hue of the image can be adjusted by converting from RGB to HSV and adjusting.
The combination of the two methods can achieve a greater degree of image tone enhancement.

Adjust hue value

__author__ = 'TracelessLe'
__website__ = 'https://blog.csdn.net/TracelessLe'

import numpy as np
from PIL import Image, ImageEnhance

filename = 'test.png'
pil_img = Image.open(filename).convert('RGB')
hue = np.random.randint(0, 360)
out = hueChange(pil_img, hue/360.) # adjust the hue value
out.save('out.png')

The hueChange method is implemented as follows:

__author__ = 'TracelessLe'
__website__ = 'https://blog.csdn.net/TracelessLe'

import numpy as np
from PIL import Image

def rgb_to_hsv(rgb):
    # Translated from source of colorsys.rgb_to_hsv
    # r,g,b should be a numpy array with values between 0 and 255
    # rgb_to_hsv returns an array of floats between 0.0 and 1.0.
    rgb = rgb.astype('float')
    hsv = np.zeros_like(rgb)
    # in case an RGBA array was passed, just copy the A channel
    hsv[..., 3:] = rgb[..., 3:]
    r, g, b = rgb[..., 0], rgb[..., 1], rgb[..., 2]
    maxc = np.max(rgb[..., :3], axis=-1)
    minc = np.min(rgb[..., :3], axis=-1)
    hsv[..., 2] = maxc
    mask = maxc != minc
    hsv[mask, 1] = (maxc - minc)[mask] / maxc[mask]
    rc = np.zeros_like(r)
    gc = np.zeros_like(g)
    bc = np.zeros_like(b)
    rc[mask] = (maxc - r)[mask] / (maxc - minc)[mask]
    gc[mask] = (maxc - g)[mask] / (maxc - minc)[mask]
    bc[mask] = (maxc - b)[mask] / (maxc - minc)[mask]
    hsv[..., 0] = np. select(
        [r == maxc, g == maxc], [bc - gc, 2.0 + rc - bc], default=4.0 + gc - rc)
    hsv[..., 0] = (hsv[..., 0] / 6.0) % 1.0
    return hsv

def hsv_to_rgb(hsv):
    # Translated from source of colorsys.hsv_to_rgb
    # h,s should be a numpy array with values between 0.0 and 1.0
    # v should be a numpy array with values between 0.0 and 255.0
    # hsv_to_rgb returns an array of uints between 0 and 255.
    rgb = np.empty_like(hsv)
    rgb[..., 3:] = hsv[..., 3:]
    h, s, v = hsv[..., 0], hsv[..., 1], hsv[..., 2]
    i = (h * 6.0).astype('uint8')
    f = (h * 6.0) - i
    p = v * (1.0 - s)
    q = v * (1.0 - s * f)
    t = v * (1.0 - s * (1.0 - f))
    i = i % 6
    conditions = [s == 0.0, i == 1, i == 2, i == 3, i == 4, i == 5]
    rgb[..., 0] = np. select(conditions, [v, q, p, p, t, v], default=v)
    rgb[..., 1] = np. select(conditions, [v, v, v, q, p, p], default=t)
    rgb[..., 2] = np. select(conditions, [v, p, t, v, v, q], default=p)
    return rgb.astype('uint8')

def hueChange(img, hue):
    arr = np.array(img)
    hsv = rgb_to_hsv(arr)
    hsv[..., 0] = hue
    rgb = hsv_to_rgb(hsv)
    return Image.fromarray(rgb, 'RGB')

See reference [1] for the principle of hue adjustment based on the hue value.

The source code here can be found in reference [2].

Adjust image chroma based on ImageEnhance method

__author__ = 'TracelessLe'
__website__ = 'https://blog.csdn.net/TracelessLe'

import random
import numpy as np
from PIL import Image, ImageEnhance

filename = 'test.png'
pil_img = Image.open(filename).convert('RGB')
enh_col = ImageEnhance.Color(pil_img)
factor = random. random() * 1.0 + 0.5
out = enh_col.enhance(factor)
out.save('out.png')

Merge operation

__author__ = 'TracelessLe'
__website__ = 'https://blog.csdn.net/TracelessLe'

import random
import numpy as np
from PIL import Image, ImageEnhance


def rgb_to_hsv(rgb):
    # Translated from source of colorsys.rgb_to_hsv
    # r,g,b should be a numpy array with values between 0 and 255
    # rgb_to_hsv returns an array of floats between 0.0 and 1.0.
    rgb = rgb.astype('float')
    hsv = np.zeros_like(rgb)
    # in case an RGBA array was passed, just copy the A channel
    hsv[..., 3:] = rgb[..., 3:]
    r, g, b = rgb[..., 0], rgb[..., 1], rgb[..., 2]
    maxc = np.max(rgb[..., :3], axis=-1)
    minc = np.min(rgb[..., :3], axis=-1)
    hsv[..., 2] = maxc
    mask = maxc != minc
    hsv[mask, 1] = (maxc - minc)[mask] / maxc[mask]
    rc = np.zeros_like(r)
    gc = np.zeros_like(g)
    bc = np.zeros_like(b)
    rc[mask] = (maxc - r)[mask] / (maxc - minc)[mask]
    gc[mask] = (maxc - g)[mask] / (maxc - minc)[mask]
    bc[mask] = (maxc - b)[mask] / (maxc - minc)[mask]
    hsv[..., 0] = np. select(
        [r == maxc, g == maxc], [bc - gc, 2.0 + rc - bc], default=4.0 + gc - rc)
    hsv[..., 0] = (hsv[..., 0] / 6.0) % 1.0
    return hsv

def hsv_to_rgb(hsv):
    # Translated from source of colorsys.hsv_to_rgb
    # h,s should be a numpy array with values between 0.0 and 1.0
    # v should be a numpy array with values between 0.0 and 255.0
    # hsv_to_rgb returns an array of uints between 0 and 255.
    rgb = np.empty_like(hsv)
    rgb[..., 3:] = hsv[..., 3:]
    h, s, v = hsv[..., 0], hsv[..., 1], hsv[..., 2]
    i = (h * 6.0).astype('uint8')
    f = (h * 6.0) - i
    p = v * (1.0 - s)
    q = v * (1.0 - s * f)
    t = v * (1.0 - s * (1.0 - f))
    i = i % 6
    conditions = [s == 0.0, i == 1, i == 2, i == 3, i == 4, i == 5]
    rgb[..., 0] = np. select(conditions, [v, q, p, p, t, v], default=v)
    rgb[..., 1] = np. select(conditions, [v, v, v, q, p, p], default=t)
    rgb[..., 2] = np. select(conditions, [v, p, t, v, v, q], default=p)
    return rgb.astype('uint8')

def hueChange(img, hue):
    arr = np.array(img)
    hsv = rgb_to_hsv(arr)
    hsv[..., 0] = hue
    rgb = hsv_to_rgb(hsv)
    return Image.fromarray(rgb, 'RGB')


if __name__ == "__main__":
filename = 'test.png'
pil_img = Image.open(filename).convert('RGB')
hue = np.random.randint(0, 360)
pil_img2 = hueChange(pil_img, hue/360.)
enh_col = ImageEnhance.Color(pil_img2)
factor = random. random() * 1.0 + 0.5
out = enh_col.enhance(factor)
out.save('out.png')
\t

Copyright statement

This article is an original article, published exclusively on blog.csdn.net/TracelessLe. Not to be reproduced without the permission of the individual. If you need help, please email to [email protected] or scan the QR code in the personal introduction column for consultation.

References

[1] Image color knowledge and python to achieve image tone conversion – Know about
[2] RGB to HSV Python, change Hue continuously – Stack Overflow
[3] [python image processing] image enhancement (Detailed explanation of ImageEnhance class)_PHILOS_THU’s Blog-CSDN Blog
[4] How to use Python to change the color of photos_VIP_CQCRE’s Blog-CSDN Blog
[5] ImageEnhance Module – Pillow (PIL Fork) 9.5.0 documentation