Python調(diào)整圖像hue值結(jié)合ImageEnhance庫(kù)以實(shí)現(xiàn)色調(diào)增強(qiáng)
前言
PIL庫(kù)中的ImageEnhance類(lèi)可用于圖像增強(qiáng),可以調(diào)節(jié)圖像的亮度、對(duì)比度、色度和銳度。
通過(guò)RGB到HSV的變換加調(diào)整可以對(duì)圖像的色調(diào)進(jìn)行調(diào)整。 兩種方法結(jié)合可以達(dá)到更大程度的圖像色調(diào)增強(qiáng)。
調(diào)整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.) # 調(diào)整hue值 out.save('out.png')
其中 hueChange 方法實(shí)現(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方法調(diào)節(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')
到此這篇關(guān)于Python調(diào)整圖像hue值結(jié)合ImageEnhance庫(kù)以實(shí)現(xiàn)色調(diào)增強(qiáng)的文章就介紹到這了,更多相關(guān)ImageEnhance庫(kù)實(shí)現(xiàn)色調(diào)增強(qiáng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談python腳本設(shè)置運(yùn)行參數(shù)的方法
今天小編就為大家分享一篇淺談python腳本設(shè)置運(yùn)行參數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12pandas 使用apply同時(shí)處理兩列數(shù)據(jù)的方法
下面小編就為大家分享一篇pandas 使用apply同時(shí)處理兩列數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04如何解決cmd運(yùn)行python提示不是內(nèi)部命令
在本篇文章里小編給大家整理了關(guān)于如何解決cmd運(yùn)行python提示不是內(nèi)部命令的相關(guān)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2020-07-07Python中內(nèi)置的日志模塊logging用法詳解
Python的logging模塊提供了記錄程序運(yùn)行情況的日志功能,類(lèi)似于Apache的log4j,很好很強(qiáng)大,這里我們就來(lái)看一下Python中內(nèi)置的日志模塊logging用法詳解2016-07-07使用Python三角函數(shù)公式計(jì)算三角形的夾角案例
這篇文章主要介紹了使用Python三角函數(shù)公式計(jì)算三角形的夾角案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04使用Django清空數(shù)據(jù)庫(kù)并重新生成
這篇文章主要介紹了使用Django清空數(shù)據(jù)庫(kù)并重新生成,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04