Sunday, December 06, 2009

Processingで画像をグラデーションのかかった画像に変換する

Processingで画像をグラデーションのかかった画像に変換するには、以下のコードを実行します。


// キャンバスサイズ
size(200, 200);
color c1 = color(#FFFF10);
color c2 = color(#3070A0);

int sx = 200;
int sy = 200;

float dr = (red(c2) - red(c1))/sy;
float dg = (green(c2) - green(c1))/sy;
float db = (blue(c2) - blue(c1))/sy;

for(int ly=0;ly<sy;ly++){
color pc = color(
(red(c1) + ly*dr),
(green(c1) + ly*dg),
(blue(c1) + ly*db)
);
for(int lx=0;lx<sx;lx++){
set(lx, ly, pc);
}
}
// オフスクリーンバッファを作成
PGraphics gr = createGraphics(200, 200, JAVA2D);
gr.colorMode(RGB, 255, 255, 255, 255);
// 画像はdataディレクトリに置いておく
PImage img = loadImage("SF.JPG");
img.filter(GRAY);
// 画像の表示
gr.beginDraw();
gr.image(img, 0, 0);
gr.loadPixels();
for(int li=0;li<img.width*img.height;li++){
gr.pixels[li] =
color(red(img.pixels[li]),
green(img.pixels[li]),
blue(img.pixels[li]),
128);
}
gr.updatePixels();
gr.endDraw();
image(gr, 0, 0);

// ファイル保存
save("sample1385a.png");


元画像(SF.JPG)


出力画像(sample1385a.png)
Processingでグラデーションをかけた画像

○動作環境
Processing 3.4/Processing 1.0.7

〇関連項目
Processingをインストールした仮想マシンを構築するには、以下のページを参照してください。
https://serverarekore.blogspot.com/search/label/Processing
・Processingに関する他の記事は、こちらを参照してください。

No comments: