前言
PIL库中的ImageEnhance类可用于图像增强,可以调节图像的亮度、对比度、色度和锐度。
通过RGB到HSV的变换加调整可以对图像的色调进行调整。
两种方法结合可以达到更大程度的图像色调增强。
调整hue值
__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.) # 调整hue值
out.save('out.png')
其中hueChange
方法实现如下:
__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 arrays 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 arrays 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')
关于基于hue值的色调调整原理可见参考资料[1]。
此处实现源码可见参考资料[2]。
基于ImageEnhance方法调节图像色度
__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')
合并操作
__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 arrays 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 arrays 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')
版权说明
本文为原创文章,独家发布在blog.csdn.net/TracelessLe。未经个人允许不得转载。如需帮助请email至tracelessle@163.com或扫描个人介绍栏二维码咨询。
参考资料
[1] 图像色彩知识及python实现图片色调转换 - 知乎
[2] RGB to HSV Python, change Hue continuously - Stack Overflow
[3] 【python图像处理】图像的增强(ImageEnhance类详解)_PHILOS_THU的博客-CSDN博客
[4] 如何用 Python 给照片换色_VIP_CQCRE的博客-CSDN博客
[5] ImageEnhance Module - Pillow (PIL Fork) 9.5.0 documentation