If I had a RGB decimal such as 255, 165, 0
, what could I do to convert this to CMYK?
如果我有一个RGB小数,比如255,165,0,我能做什么来把它转换成CMYK?
For example:
例如:
>>> red, green, blue = 255, 165, 0
>>> rgb_to_cmyk(red, green, blue)
(0, 35, 100, 0)
6 个解决方案
#1
7
Here's a Python port of a Javascript implementation.
这里是一个Javascript实现的Python端口。
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
#2
3
Following up on Mr. Fooz's implementation.
跟踪Fooz先生的执行情况。
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).
CMYK有两种可能的实现。有比例的一个关于空格(例如用于GIMP),这是一个实现Fooz先生,但也有另一个实现CMYK(例如使用LibreOffice)使色彩比例对总色彩空间。如果你想用CMYK来模拟颜料和墨水的混合,那么第二种颜色可能会更好,因为颜色可以用每一种颜色的重量线性加起来(0.5个半混合)。
Here is the second version of CMYK with back conversion:
这是CMYK的第二个版本,后面是转换:
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
#3
2
For this conversion to be useful, you need a color management system, with profiles describing the RGB system and the CMYK system being converted.
为了使这个转换有用,您需要一个颜色管理系统,配置文件描述RGB系统和正在转换的CMYK系统。
http://en.wikipedia.org/wiki/CMYK_color_model#Conversion
http://en.wikipedia.org/wiki/CMYK_color_model转换
Here is a discussion of how to solve this problem using ICC profiles:
这里有一个关于如何使用ICC配置文件解决这个问题的讨论:
如何在一组任意像素值(而不是图像数据结构)上使用ICC配置文件执行颜色转换?
Here is a link to pyCMS, which uses ICC color profiles to do the conversion:
这是一个与pyCMS的链接,它使用ICC颜色配置文件进行转换:
http://www.cazabon.com/pyCMS/
#4
0
I tried using the back computation provided by bisounours_tronconneuse and it failed for CMYK (96 63 0 12). Result should be : like this
我尝试使用bisounours_tronconneuse提供的back计算,它失败了CMYK(96 663 0 12)。结果应该是:像这样。
Converting w3schools javascript (code here) to python, the below code now returns correct results:
将w3schools javascript(这里的代码)转换为python,下面的代码将返回正确的结果:
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)
#5
0
The accepted answer provided a nice way to go from RGB to CMYK but question title also includes
这个被接受的答案提供了一个从RGB到CMYK的好方法,但是问题标题也包括了。
vice versa
反之亦然
So here's my contribution for conversion from CMYK to RGB:
这是我从CMYK转换到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.
与patapouf_ai的答案不同,这个函数不会产生负的rgb值。
#6
-1
But converting full image RGB2CMYK or vice versa is as simple as
但是转换完整的图像RGB2CMYK或反之亦然。
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')
#1
7
Here's a Python port of a Javascript implementation.
这里是一个Javascript实现的Python端口。
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
#2
3
Following up on Mr. Fooz's implementation.
跟踪Fooz先生的执行情况。
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).
CMYK有两种可能的实现。有比例的一个关于空格(例如用于GIMP),这是一个实现Fooz先生,但也有另一个实现CMYK(例如使用LibreOffice)使色彩比例对总色彩空间。如果你想用CMYK来模拟颜料和墨水的混合,那么第二种颜色可能会更好,因为颜色可以用每一种颜色的重量线性加起来(0.5个半混合)。
Here is the second version of CMYK with back conversion:
这是CMYK的第二个版本,后面是转换:
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
#3
2
For this conversion to be useful, you need a color management system, with profiles describing the RGB system and the CMYK system being converted.
为了使这个转换有用,您需要一个颜色管理系统,配置文件描述RGB系统和正在转换的CMYK系统。
http://en.wikipedia.org/wiki/CMYK_color_model#Conversion
http://en.wikipedia.org/wiki/CMYK_color_model转换
Here is a discussion of how to solve this problem using ICC profiles:
这里有一个关于如何使用ICC配置文件解决这个问题的讨论:
如何在一组任意像素值(而不是图像数据结构)上使用ICC配置文件执行颜色转换?
Here is a link to pyCMS, which uses ICC color profiles to do the conversion:
这是一个与pyCMS的链接,它使用ICC颜色配置文件进行转换:
http://www.cazabon.com/pyCMS/
#4
0
I tried using the back computation provided by bisounours_tronconneuse and it failed for CMYK (96 63 0 12). Result should be : like this
我尝试使用bisounours_tronconneuse提供的back计算,它失败了CMYK(96 663 0 12)。结果应该是:像这样。
Converting w3schools javascript (code here) to python, the below code now returns correct results:
将w3schools javascript(这里的代码)转换为python,下面的代码将返回正确的结果:
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)
#5
0
The accepted answer provided a nice way to go from RGB to CMYK but question title also includes
这个被接受的答案提供了一个从RGB到CMYK的好方法,但是问题标题也包括了。
vice versa
反之亦然
So here's my contribution for conversion from CMYK to RGB:
这是我从CMYK转换到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.
与patapouf_ai的答案不同,这个函数不会产生负的rgb值。
#6
-1
But converting full image RGB2CMYK or vice versa is as simple as
但是转换完整的图像RGB2CMYK或反之亦然。
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')