Python調整圖像hue值結合ImageEnhance庫以實現(xiàn)色調增強
更新時間:2023年09月23日 09:45:50 作者:TracelessLe
這篇文章主要介紹了Python調整圖像hue值結合ImageEnhance庫以實現(xiàn)色調增強,PIL庫中的ImageEnhance類可用于圖像增強,可以調節(jié)圖像的亮度、對比度、色度和銳度,通過RGB到HSV的變換加調整可以對圖像的色調進行調整,需要的朋友可以參考下
前言
PIL庫中的ImageEnhance類可用于圖像增強,可以調節(jié)圖像的亮度、對比度、色度和銳度。

通過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 方法實現(xiàn)如下:
__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')基于ImageEnhance方法調節(jié)圖像色度

__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')到此這篇關于Python調整圖像hue值結合ImageEnhance庫以實現(xiàn)色調增強的文章就介紹到這了,更多相關ImageEnhance庫實現(xiàn)色調增強內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
教你怎么用PyCharm為同一服務器配置多個python解釋器
當我們在服務器上創(chuàng)建了多個虛擬環(huán)境時,也可以在 PyCharm 中配置這些虛擬環(huán)境,方便不同的項目使用不同的環(huán)境,然而按照網上教程添加多個python解釋器后,PyCharm會自動幫我們創(chuàng)建多個重復的服務器,本文主要給出該問題的解決方法,同時也對添加解釋器做一個詳細的講解2021-05-05
Python opencv缺陷檢測的實現(xiàn)及問題解決
這篇文章主要介紹了Python opencv缺陷檢測的實現(xiàn)及問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
python3.8動態(tài)人臉識別的實現(xiàn)示例
這篇文章主要介紹了python3.8動態(tài)人臉識別的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09

