3

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.

AvrDragon
  • 7,139
  • 4
  • 27
  • 42
  • If you are trying to get a grey-scale image, wouldn't the output be just the K channel? I don't quite understand CMYK so please explain to me if I'm wrong. – Hawken Aug 24 '13 at 17:24
  • @Hawken no, max CMY is black to, so there is many ways to represent a color in CMYK. I mean (1, 1, 1, 0) and (0, 0, 0, 1) theoretically is the same black color. – AvrDragon Aug 27 '13 at 12:24
  • true, but if they are the same color, using only black ink would save you ink wouldn't it? Also certain printers use a different black ink when doing grayscale; perhaps only using the 'K' channel might trigger this? – Hawken Sep 05 '13 at 01:55
  • @Hawken yes, but the question is about converting image in CMYK. And you can not assume that this image is optimized for ink. So if you have a CMY image, where K channel ist empty, it should be converted correctly too. – AvrDragon Sep 10 '13 at 11:30

1 Answers1

4

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.

mmgp
  • 18,901
  • 3
  • 53
  • 80
  • actually converting form cmyk to rgb is not that easy and requeres icc_profile, but the "simple" formula works +- fine too. – AvrDragon Feb 25 '13 at 16:17