from PIL import Image,ImageColor
# from torchvision.utils import make_grid
import random
from functools import partial
def func(x,c):
return x+c if x+c <256 else 255
def adj_hue(img):
hue,s,v = img.split()
f1 = partial(func,c=random.randint(-20,20))
f2 = partial(func,c=random.randint(-20,20))
hue = Image.eval(hue,f1)
s = Image.eval(s, f2)
v = Image.eval(v, lambda x: (x/255)**0.5*255 ) #gamma 矫正
# hue = Image.eval(hue, lambda x: x - 20 if x - 20 < 256 else 255)
# s = Image.eval(s, lambda x: x - 20 if x - 20 < 256 else 255)
# v = Image.eval(v, lambda x: x - 20 if x - 20 < 256 else 255)
return Image.merge(img.mode,[hue,s,v])
with open("D:\ppt_about\imgs/face_src.jpg","rb") as f:
img_src = Image.open(f)
img_hsv = img_src.convert('HSV')
print(tuple(img_src.getdata())[0:10])
print(tuple(img_hsv.getdata())[0:10])
img_hsv = adj_hue(img_hsv)
print(tuple(img_hsv.getdata())[0:10])
img_hsv = img_hsv.convert('RGB')
print(img_hsv.mode)
img_hsv.save("hue_adj.jpg")
PIL实现色相变换
最新推荐文章于 2022-03-25 23:12:31 发布