简体   繁体   中英

How to change the HSV values of an image in python?

I am able to convert an image from the RGB to HSV colorspace, but how can I then manipulate those values using the HSV scale as outlined in the PIL documentation ?

img = Image.open("assets/image.png")
img = img.convert('HSV')
img.show()

You can convert the image to a NumPy array and manipulate it from there.

For example, to shift the hue:

import numpy as np
from PIL import image

def hue_shift(img, amount):
    hsv_img = img.convert('HSV')
    hsv = np.array(hsv_img)
    hsv[..., 0] = (hsv[..., 0]+amount) % 360
    new_img = Image.fromarray(hsv, 'HSV')
    return new_img.convert('RGB')
If you get HSV images, you can use opencv library.

import argparse.
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i", "-image", required = True, help = "Path to the image");
args = vars(ap.parse_args());
image = cv2.imread(args["image"]);
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM