Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 240
Michael Avatar asked Dec 27 '25 16:12

Michael


1 Answers

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')
like image 99
Agargara Avatar answered Dec 30 '25 06:12

Agargara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!