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, 165, 0 >>> rgb_to_cmyk(red, green, blue) (0, 35, 100, 0)
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, 165, 0 >>> rgb_to_cmyk(red, green, blue) (0, 35, 100, 0)
Here's a Python port of a Javascript implementation.
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 / 255. m = 1 - g / 255. y = 1 - b / 255. # 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
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
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:
Here is a link to pyCMS, which uses ICC color profiles to do the conversion:
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)
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.
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')