Help us understand the problem. What is going on with this article?

Processingで部分的に画像を透過させる方法

More than 1 year has passed since last update.

背景

QAサイトで半年以上前の質問に対して回答しても当然誰も見てくれなくて悲しかったので、掲載する。

やりたいこと

Processingで画像データを読み込み、任意の領域を任意の透過量で透過させたい。

実装

コードは以下。画像の各ピクセルの色を抽出し、透過量を設定したうえで各ピクセルを描画するというゴリ押しな手法です。
ピクセル描画時にellipseを使っていますが、rectまたはspotを使用しても良いです。。
spotを使用する場合は、透過量の設定はfillではなくstrokeを使用します。

PImage img;
int imgWidth, imgHeight;

//透過させたい範囲
int xstart = 100;
int xstop = 200;
int ystart = 50;
int ystop = 100;
//透過量
int tpSet = 100;

void setup() {
  size(480, 320);
  img = loadImage("imgName");
  imgWidth = img.width;
  imgHeight = img.height;
  noStroke();
  smooth();
  background(255);
  frameRate(60);
}

void draw() {
  background(255);
  img.loadPixels();
  for (int x =0; x<imgWidth; x++) {
    for (int y=0; y<imgHeight; y++) {
      //x,yが選択した範囲であれば透過させる。
      color imgColor = img.get(x,y);
      if(x>xstart && x<xstop && y>ystart && y<ystop){
        fill(imgColor, tpSet);
      }else{
        fill(imgColor);
      }
      ellipse(x, y, 1, 1);//rectでもよい
    }
  }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした