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

后端 未结 7 1127
夕颜
夕颜 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 08:55

    Following up on Mr. Fooz's implementation.

    There are two possible implementations of CMYK. There is the one where the proportions are with respect to white space (which is used for example in GIMP) and which is the one implemented by Mr. Fooz, but there is also another implementation of CMYK (used for example by LibreOffice) which gives the colour proportions with respect to the total colour space. And if you wish to use CMYK to model the mixing of paints or inks, than the second one might be better because colours can just be linearly added together using weights for each colour (0.5 for a half half mixture).

    Here is the second version of CMYK with back conversion:

    rgb_scale = 255
    cmyk_scale = 100
    
    
    def rgb_to_cmyk(r,g,b):
        if (r == 0) and (g == 0) and (b == 0):
            # black
            return 0, 0, 0, cmyk_scale
    
        # rgb [0,255] -> cmy [0,1]
        c = 1 - r / float(rgb_scale)
        m = 1 - g / float(rgb_scale)
        y = 1 - b / float(rgb_scale)
    
        # extract out k [0,1]
        min_cmy = min(c, m, y)
        c = (c - min_cmy) 
        m = (m - min_cmy) 
        y = (y - 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
    
    def cmyk_to_rgb(c,m,y,k):
        """
        """
        r = rgb_scale*(1.0-(c+k)/float(cmyk_scale))
        g = rgb_scale*(1.0-(m+k)/float(cmyk_scale))
        b = rgb_scale*(1.0-(y+k)/float(cmyk_scale))
        return r,g,b
    
    0 讨论(0)
  • 2020-12-01 08:55

    Using a CMYK conversion like the one given in the accepted answer (at the time of this writing) is not accurate for most practical purposes.

    CMYK is based on how four kinds of ink form colors on paper; however, color mixture of inks is considerably complex, more so than the mixture of "lights" used to form colors in the RGB color model.

    As CMYK is useful, above all, when printing images, any conversion to CMYK needs to take the printing condition into account, including what printer and what paper is used for printing. An accurate conversion to CMYK for printing purposes is not trivial and requires calibrating the printer and measuring CMYK patches on a test sheet, among other things.

    There is no meaning for CMYK colors that is as ubiquitous as sRGB is for RGB, as illustrated by the International Color Consortium's page of CMYK characterization data.

    See also my color article on this subject.

    0 讨论(0)
  • 2020-12-01 08:58

    For this conversion to be useful, you need a color management system, with profiles describing the RGB system and the CMYK system being converted.

    http://en.wikipedia.org/wiki/CMYK_color_model#Conversion

    Here is a discussion of how to solve this problem using ICC profiles:

    How can one perform color transforms with ICC profiles on a set of arbitrary pixel values (not on an image data structure)?

    Here is a link to pyCMS, which uses ICC color profiles to do the conversion:

    http://www.cazabon.com/pyCMS/

    0 讨论(0)
  • 2020-12-01 09:00

    I tried using the back computation provided by bisounours_tronconneuse and it failed for CMYK (96 63 0 12). Result should be : like this

    Converting w3schools javascript (code here) to python, the below code now returns correct results:

    def cmykToRgb(c, m, y, k) :
        c=float(c)/100.0
        m=float(m)/100.0
        y=float(y)/100.0
        k=float(k)/100.0
        r = round(255.0 - ((min(1.0, c * (1.0 - k) + k)) * 255.0))
        g = round(255.0 - ((min(1.0, m * (1.0 - k) + k)) * 255.0))
        b = round(255.0 - ((min(1.0, y * (1.0 - k) + k)) * 255.0))
        return (r,g,b)
    
    0 讨论(0)
  • 2020-12-01 09:05

    But converting full image RGB2CMYK or vice versa is as simple as

    from PIL import Image
    image = Image.open(path_to_image)
    
    if image.mode == 'CMYK':
        rgb_image = image.convert('RGB')
    
    if image.mode == 'RGB':
        cmyk_image = image.convert('CMYK')
    
    0 讨论(0)
  • 2020-12-01 09:09

    The accepted answer provided a nice way to go from RGB to CMYK but question title also includes

    vice versa

    So here's my contribution for conversion from CMYK to RGB:

    def cmyk_to_rgb(c, m, y, k, cmyk_scale, rgb_scale=255):
        r = rgb_scale * (1.0 - c / float(cmyk_scale)) * (1.0 - k / float(cmyk_scale))
        g = rgb_scale * (1.0 - m / float(cmyk_scale)) * (1.0 - k / float(cmyk_scale))
        b = rgb_scale * (1.0 - y / float(cmyk_scale)) * (1.0 - k / float(cmyk_scale))
        return r, g, b
    

    Unlike patapouf_ai's answer, this function doesn't result in negative rgb values.

    0 讨论(0)
提交回复
热议问题