# Convert color of an image from rgb to cmyk

**URL:** https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922
**Category:** Coding Questions
**Created:** [August 24, 2018, 11:29am UTC](https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922 "2018-08-24T11:29:57Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)
#### Post date: [August 24, 2018, 11:29am UTC](https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922/1 "2018-08-24T11:29:57Z")

</div>

hi guys  
i’m tryng to convert rgb values to cmyk values.

i wrote this code, i think that’s right.

```auto
class Cmyk {
  PImage img;
  float[][] c, m, y, k, r1, g1, b1;

  Cmyk(PImage img) {
    this.img=img;
    r1 = new float[img.width][img.height];
    g1 = new float[img.width][img.height];
    b1 = new float[img.width][img.height];
    c = new float[img.width][img.height];
    m = new float[img.width][img.height];
    y = new float[img.width][img.height];
    k = new float[img.width][img.height];
  }

  void newRgbValue() {
    for (int i=0; i<img.width; i++) {
      for (int j=0; j<img.height; j++) {
        r1[i][j]=red(img.pixels[i+j*width])/255.;
        g1[i][j]=green(img.pixels[i+j*width])/255.;
        b1[i][j]=blue(img.pixels[i+j*width])/255.;
      }
    }
  }

  void findK() {
    for (int i=0; i<img.width; i++) {
      for (int j=0; j<img.height; j++) {
        k[i][j]=1-max(r1[i][j], g1[i][j], b1[i][j]);
      }
    }
  }

  void convert() {
    newRgbValue();
    findK();
    
    for (int i=0; i<img.width; i++) {
      for (int j=0; j<img.height; j++) {
        c[i][j]=(1-r1[i][j]-k[i][j])/(1-k[i][j]);
        m[i][j]=(1-g1[i][j]-k[i][j])/(1-k[i][j]);
        y[i][j]=(1-b1[i][j]-k[i][j])/(1-k[i][j]);
      }
    }
  }
}

```

i think that the values are right…is there a way to create in processing an image with cmyk value instead rgb or hsb?

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 24, 2018, 12:07pm UTC](https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922/2 "2018-08-24T12:07:45Z")

</div>

Hello,

Processing only support RGB and HSB mode : [https://processing.org/reference/colorMode\_.html](https://processing.org/reference/colorMode_.html)

So you can’t directly check if your algorithm is correct.  
Maybe you can find a way to export the picture and see if this is correct.  
Or try to convert 10 or so RGB color to CMYK and then use an online converter to check if your results are correct.

---

<div class="post-metadata">

### Author: ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)
#### Post date: [August 24, 2018, 12:11pm UTC](https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922/3 "2018-08-24T12:11:08Z")

</div>

thank you for the reply! unfortunately I imagined that processing did not support these images. do you know if there is a library? my online research did not give results.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 24, 2018, 12:14pm UTC](https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922/4 "2018-08-24T12:14:02Z")

</div>

I do not know…

What are you trying to do exactly? Since you already a code to convert it, what is missing?

---

<div class="post-metadata">

### Author: ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)
#### Post date: [August 24, 2018, 12:23pm UTC](https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922/5 "2018-08-24T12:23:52Z")

</div>

i need to have the single channel cmyk, i just tried to recombine the single image exported in photoshop and the result looks wrong. So i don’t know if the code is right or not!

---

<div class="post-metadata">

### Author: ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)
#### Post date: [August 24, 2018, 12:26pm UTC](https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922/6 "2018-08-24T12:26:09Z")

</div>

I believe that normalization to 255 after conversion is wrong and produces wrong colors

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 24, 2018, 12:34pm UTC](https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922/7 "2018-08-24T12:34:48Z")

</div>

So why don’t your try your code with just few selected colors and check if your output is the correct one using this website for exemple: [https://www.rapidtables.com/convert/color/rgb-to-cmyk.html](https://www.rapidtables.com/convert/color/rgb-to-cmyk.html)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [August 24, 2018, 12:38pm UTC](https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922/8 "2018-08-24T12:38:01Z")

</div>

> <https://stackoverflow.com/questions/4858131/rgb-to-cmyk-and-back-algorithm>

---

<div class="post-metadata">

### Author: ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)
#### Post date: [August 24, 2018, 12:47pm UTC](https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922/9 "2018-08-24T12:47:42Z")

</div>

thank you for the reply, but i’m a little bit confusing. it doesn’t look work!!

```auto
Cmyk cmyk;
PImage img;

void settings() {
  img=loadImage("acqua.jpg");
  img.resize(img.width/4, img.height/4);
  size(img.width, img.height);
}

void setup() {
  cmyk=new Cmyk(img);
  noLoop();
}

void draw() {
  cmyk.convert();
  loadPixels();
  for (int i=0; i<img.width; i++) {
    for (int j=0; j<img.height; j++) {
      pixels[i+j*img.width]=color(cmyk.k[i][j]*100);
    }
  }
  updatePixels();
  saveFrame("k.jpg");
}

```

with this part of code i save the single channel. when i try to recombine it in photoshop, the image has wrong color!

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [August 26, 2019, 10:24pm UTC](https://discourse.processing.org/t/convert-color-of-an-image-from-rgb-to-cmyk/2922/10 "2019-08-26T22:24:42Z")

</div>

For a related approach to separate C M Y K channels, see:

> [@Best way to do this: tint(C, M, Y, K)?](https://discourse.processing.org/t/best-way-to-do-this-tint-c-m-y-k/5317/11):
>
> I think @Jakub is right. The reference for all the blend modes: [blendMode()](https://processing.org/reference/blendMode_.html) or maybe [blend()](https://processing.org/reference/blend_.html). You can use SUBSTRACT or MULTIPLY and try another ones. They are similar to [Photoshop blending modes](https://helpx.adobe.com/photoshop/using/blending-modes.html). Mathematically, RGB converts directly to CMY, with no need of Black (K). Theoretically, the mix of C+M+Y should result in black. We only use Black on printing because inks are not perfect, so black ink gets better results for contrast and other details. So, when you add black ink, you need to do the m…
