Question

I need to convert a CMYK image to grayscaled CMYK image. At first i thought i can just use the same method as for RGB -> grayscale conversion, like (R + G + B) / 3 or max(r, g, b) / 2 + min(r, g, b). But unfortunately this works bad for CMYK, because CMY and K are redundant. And black cmyk (0, 0, 0, 255) will become 64. Should i just use

int grayscaled = max((c + m + y) / 3, k)

or could it be more tricky? I am not sure, because CMYK is actually a redundant color model, and the same color could be encoded in a different ways.

Was it helpful?

Solution

There are several ways to convert RGB to Grayscale, the average of the channels mentioned is one of them. Using the Y channel from the colorspace YCbCr is also a common option, as well the L channel from LAB, among other options.

Converting CMYK to RGB is cheap according to the formulas in http://www.easyrgb.com/index.php?X=MATH, and then you can convert to grayscale using well known approaches. Supposing the c, m, y, and k in range [0, 1], we can convert it to grayscale luma as in:

def cmyk_to_luminance(c, m, y, k):
    c = c * (1 - k) + k
    m = m * (1 - k) + k
    y = y * (1 - k) + k

    r, g, b = (1 - c), (1 - m), (1 - y)
    y = 0.299 * r + 0.587 * g + 0.114 * b
    return y

See also http://en.wikipedia.org/wiki/Grayscale for a bit more about this conversion to grayscale.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top