Motheus
Motheus

Reputation: 543

Shift hue using Pillow

I want to know if there's an easy and proper way to shift the hue using Pillow (or an easier alternative with a different library).

I've found a couple of answer about colorizing a picture (here and here), but I don't want to do that, I just want to shift the hue so the number of colors in the image will be preserved.

Upvotes: 0

Views: 920

Answers (1)

dlt_w
dlt_w

Reputation: 378

Hue shifting can be easily done in HSV space. Using 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)

Input: input Output: output

Upvotes: 4

Related Questions