Convert color of an image from rgb to cmyk

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

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

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?

1 Like

Hello,

Processing only support RGB and HSB mode : 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.

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.

I do not know…

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

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!

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

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

1 Like

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

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!

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

1 Like