0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Coolorsが選んだ色でグレースケール画像を自動着色する(Processing)

Posted at

いいかんじの配色をピックしてくれるサービスCoolorsがにわかに話題なので、グレースケール画像をとりこんだら勝手に着色してくれるようなものをProcessingで作成しました。

CoolorsのURLからカラータグを抽出するところはこちらのプログラムを参考にさせていただきました。
Coolorsからカラーパレットを作成 by Processing


PImage img;
//任意の画像を読み込み
img = loadImage("input.jpeg"); 
//coolorsからとってきたURLをここに貼る
String url = "https://coolors.co/49306b-87bba2-635380-a581a0-d1cbc4";
color[] coolors = new color[5];
color[] cMap = new color[256];

void setup() {
  noLoop();
  //urlをカラーコードに変換
  String[] str = split(url.substring(19), "-");
  for (int i=0; i < str.length; i++) {
    int r = unhex(str[i].substring(0,2));
    int g = unhex(str[i].substring(2,4)); 
    int b = unhex(str[i].substring(4,6)); 
    coolors[i] = color(r, g, b);
  }
  //グラデーションマップを作成
  for(int i=0; i < 256; i++){
    float t = i%64 * 1.0/64;
    int n = floor(i/64);
    float r = (1-t)*red(coolors[n])+t*red(coolors[n+1]);
    float g = (1-t)*green(coolors[n])+t*green(coolors[n+1]);
    float b = (1-t)*blue(coolors[n])+t*blue(coolors[n+1]);
    cMap[i] = color(r, g, b);
  }
}

void draw(){
  img.loadPixels();
  PImage new_img = createImage( img.width, img.height, RGB );
  //グレースケールの濃淡をグラデーションマップに置き換え
  for(int y=0; y < img.height; y++){
    for(int x=0; x < img.width; x++){
      int pos = x + y*img.width;
      color c = img.pixels[pos];
      int n = int(red( c ));
      new_img.pixels[pos] = cMap[n];
    }
  }
  new_img.updatePixels();
  //作成した画像を保存
  boolean blRet;
  blRet = new_img.save("img1_1.jpeg");
  if(blRet == false){
    print("error");
  }
}

配色に悩んだ時に色々試してみるとインスピレーションの助けになるかもしれません!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?