How can I convert RGB to CMYK and vice versa in python?

后端 未结 7 1128
夕颜
夕颜 2020-12-01 08:13

If I had a RGB decimal such as 255, 165, 0, what could I do to convert this to CMYK?

For example:

>>> red, green, blue = 255, 1         


        
相关标签:
7条回答
  • 2020-12-01 09:12

    Here's a Python port of a Javascript implementation.

    RGB_SCALE = 255
    CMYK_SCALE = 100
    
    
    def rgb_to_cmyk(r, g, b):
        if (r, g, b) == (0, 0, 0):
            # black
            return 0, 0, 0, CMYK_SCALE
    
        # rgb [0,255] -> cmy [0,1]
        c = 1 - r / RGB_SCALE
        m = 1 - g / RGB_SCALE
        y = 1 - b / RGB_SCALE
    
        # extract out k [0, 1]
        min_cmy = min(c, m, y)
        c = (c - min_cmy) / (1 - min_cmy)
        m = (m - min_cmy) / (1 - min_cmy)
        y = (y - min_cmy) / (1 - min_cmy)
        k = min_cmy
    
        # rescale to the range [0,CMYK_SCALE]
        return c * CMYK_SCALE, m * CMYK_SCALE, y * CMYK_SCALE, k * CMYK_SCALE
    
    0 讨论(0)
提交回复
热议问题