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

If I had a decimal RGB, for example 255, 165, 0 , what could I do to convert it to CMYK?

For instance:

 >>> red, green, blue = 255, 165, 0 >>> rgb_to_cmyk(red, green, blue) (0, 35, 100, 0) 
+8
source share
7 answers

Here's the Python port for implementing Javascript .

 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 
+13
source

Continued implementation of Mr. Foose.

There are two possibilities for implementing CMYK. There is one where the proportions refer to a space (which is used, for example, in GIMP), and which is implemented by Mr. Foose, but there is another CMYK implementation (used, for example, by LibreOffice) that gives color proportions relative to the overall color space. And if you want to use CMYK to simulate the mixing of paints or inks than the second, it might be better because colors can simply be added linearly along with the use of weight for each color (0.5 for half of the mixture).

Here is the second version of CMYK with inverse 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 
+4
source

The accepted answer provided a good way to switch from RGB to CMYK, but the title of the question also includes

vice versa

So here is my contribution for converting 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 does not produce negative rgb values.

+4
source

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

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

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

How can I perform color conversions with ICC profiles on a set of arbitrary pixel values โ€‹โ€‹(and not on the image data structure)?

Here is a link to pyCMS that uses ICC color profiles for conversion:

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

+2
source

But converting a full RGB2CMYK image 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') 
+2
source

I tried to use the back calculation provided by bisounours_tronconneuse and it did not pass for CMYK (96 63 0 12). The result should be: like this

Converting w3schools javascript ( code here ) to python, 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) 
+1
source

Using a CMYK transform similar to the one in the accepted answer (at the time of this writing) is not suitable for most practical purposes.

CMYK is based on how four types of ink form colors on paper; however, the color mix of ink is significantly more complex than the mix of โ€œlight sourcesโ€ used to form colors in the RGB color model.

Since CMYK is useful primarily for printing images, any conversion to CMYK should take into account printing conditions, including which printer and which paper is used for printing. Accurate conversion to CMYK for printing purposes is not trivial and requires, among other things, printer calibration and measurement of CMYK patches on a test sheet.

There is no point for CMYK colors as ubiquitous as sRGB for RGB colors, as shown on the International Color Consortium page with CMYK specifications .

See also my color article on this topic.

0
source

All Articles