HSV to RGB Color Conversion

后端 未结 9 1672
离开以前
离开以前 2020-12-14 01:16

Is there a way to convert HSV color arguments to RGB type color arguments using pygame modules in python? I tried the following code, but it returns ridiculous values.

相关标签:
9条回答
  • 2020-12-14 01:43

    If you like performance, it's best to avoid imports and use your own optimized code

    Here's the exact code from colorsys slightly modified to make the byte-code slightly faster:

        def hsv_to_rgb(h, s, v):
            if s == 0.0: return (v, v, v)
            i = int(h*6.) # XXX assume int() truncates!
            f = (h*6.)-i; p,q,t = v*(1.-s), v*(1.-s*f), v*(1.-s*(1.-f)); i%=6
            if i == 0: return (v, t, p)
            if i == 1: return (q, v, p)
            if i == 2: return (p, v, t)
            if i == 3: return (p, q, v)
            if i == 4: return (t, p, v)
            if i == 5: return (v, p, q)
    

    output:

    >>> hsv_to_rgb(359,1,1)
    [1, 0.0, 0.0]
    

    Using an if-chain like above is actually faster than using elif

    Using a wrapper, like in Cyber's answer, takes a few extra steps for the interpreter to perform.
    To add, the for loop in Cyber's example is a real performance killer when used like that

    If you want slightly more performance, simply do this:
    (I won't say this is the best possible performance, but it's certainly better)

        def hsv_to_rgb(h, s, v):
            if s == 0.0: v*=255; return (v, v, v)
            i = int(h*6.) # XXX assume int() truncates!
            f = (h*6.)-i; p,q,t = int(255*(v*(1.-s))), int(255*(v*(1.-s*f))), int(255*(v*(1.-s*(1.-f)))); v*=255; i%=6
            if i == 0: return (v, t, p)
            if i == 1: return (q, v, p)
            if i == 2: return (p, v, t)
            if i == 3: return (p, q, v)
            if i == 4: return (t, p, v)
            if i == 5: return (v, p, q)
    

    ^ this guarantees int() output with a range of 255 (the input is still the same)

    >>> hsv_to_rgb(359./360.,1,1)
    (255, 0, 0)
    

    TIP: stay away from 3rd-party where possible, try the direct approach if you can.
    exculusions: compiled C extensions such as PIL or NumPy, or ctypes wrappers such as PyOpenGL (uses the DLL)

    0 讨论(0)
  • 2020-12-14 01:45

    I found the following code to work with images represented as numpy ndarrays:

    from skimage.io import imread
    import matplotlib.colors as mcolors
    img = imread( 'my_image.png' )
    img_hsv = mcolors.rgb_to_hsv( img )
    img_hsv = img_hsv / (1.0, 1.0, 255.0)
    

    The last division was useful to convert to a floating representation between 0.0 and 1.0, as for some reason the last component originally ranged between 0 and 255.

    0 讨论(0)
  • 2020-12-14 01:48

    I have prepared a vectorized version, it is cca 10x faster

    def hsv_to_rgb(h, s, v):
        shape = h.shape
        i = int_(h*6.)
        f = h*6.-i
    
        q = f
        t = 1.-f
        i = ravel(i)
        f = ravel(f)
        i%=6
    
        t = ravel(t)
        q = ravel(q)
    
        clist = (1-s*vstack([zeros_like(f),ones_like(f),q,t]))*v
    
        #0:v 1:p 2:q 3:t
        order = array([[0,3,1],[2,0,1],[1,0,3],[1,2,0],[3,1,0],[0,1,2]])
        rgb = clist[order[i], arange(prod(shape))[:,None]]
    
        return rgb.reshape(shape+(3,))
    
    0 讨论(0)
提交回复
热议问题