Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RGB to HSV via PIL and colorsys

I have written a function which converts an Image from RGB > HSV. However, when I save the new image the only thing I get is a black image. What should I fix to get it solved?

Any help is kindly appreciated!

My code:

def HSVColor(img):
    if isinstance(img,Image):
        r,g,b = img.split()
        Hdat = []
        Sdat = []
        Vdat = [] 
        for rd,gn,bl in zip(r.getdata(),g.getdata(),b.getdata()) :
            h,s,v = colorsys.rgb_to_hsv(rd/255.,gn/255.,bl/255.)
            Hdat.append(int(h*255.))
            Sdat.append(int(l*255.))
            Vdat.append(int(s*255.))
        r.putdata(Hdat)
        g.putdata(Sdat)
        b.putdata(Vdat)
        return Image.merge('RGB',(r,g,b))
    else:
        return None
like image 416
bolzano Avatar asked Nov 27 '22 13:11

bolzano


2 Answers

Just FYI, with a recent copy of Pillow, one should probably use

def rgb2hsv(image):
    return image.convert('HSV')
like image 137
K3---rnc Avatar answered Dec 15 '22 10:12

K3---rnc


i think you are trying to do the impossible, although you can transform rgb to hsv colour (and vice versa) values, according to wikipedia jpg images can only be stored in rgb or cmyk models.

like image 37
Tony Cronin Avatar answered Dec 15 '22 12:12

Tony Cronin