【问题标题】:Shift hue using Pillow使用 Pillow 改变色调
【发布时间】:2021-11-21 08:08:35
【问题描述】:
我想知道是否有一种使用 Pillow 来改变色调的简单而正确的方法(或者使用不同库的更简单的替代方法)。
我找到了一些关于为图片着色的答案(here 和 here),但我不想这样做,我只想改变色调,以便图像中的颜色数量将被保留。
【问题讨论】:
标签:
python
image-processing
python-imaging-library
【解决方案1】:
在 HSV 空间中可以轻松地进行色相转换。使用 OpenCV:
import cv2
import numpy as np
img_bgr = cv2.imread('hue_wheel.jpg')
# convert image from RGB (OpenCV uses BGR order) color space to HSV space
img_hsv = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HSV)
# H values are in range 0 to 180
# check: https://docs.opencv.org/master/de/d25/imgproc_color_conversions.html#color_convert_rgb_hsv
# shift hue by 180 deg or any other value
n_shift = 180 // 2
img_hsv[:, :, 0] = (img_hsv[:, :, 0].astype(np.int) + n_shift) % 181
# convert hue shifted image back to RGB space
img_bgr_modified = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite('hue_wheel_modified.jpg', img_bgr_modified)
输入: 输出: