[JAVA] How to make an image partially transparent in Processing

background

Even if I answered the question more than half a year ago on the QA site, I was sad that no one saw it, so I will post it.

Thing you want to do

I want to read image data with Processing and make an arbitrary area transparent with an arbitrary amount of transparency.

Implementation

The code is below. It is a tricky technique to extract the color of each pixel of the image, set the amount of transparency, and then draw each pixel. I use ellipse when drawing pixels, but you can also use rect or spot. .. When using spot, the transparency setting uses stroke instead of fill.

PImage img;
int imgWidth, imgHeight;

//Range you want to be transparent
int xstart = 100;
int xstop = 200;
int ystart = 50;
int ystop = 100;
//Permeation amount
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,If y is the selected range, make it transparent.
      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);//May be rect
    }
  }
}

Recommended Posts

How to make an image partially transparent in Processing
[Rails] How to display an image in the view
How to crop an image with libGDX
How to blur an image (super easy)
How to make an image posted using css look like an icon
How to implement asynchronous processing in Outsystems
[Swift] How to set an image in the background without using UIImageView.
How to make Spring Boot Docker Image smaller
How to solve an Expression Problem in Java
How to make a follow function in Rails
How to build an executable jar in Maven
How to make an crazy Android music player
How to make shaded-jar
How to make a judgment method to search for an arbitrary character in an array
How to start a subscript from an arbitrary number in Ruby iterative processing
How to make batch processing with Rails + Heroku configuration
How to make an oleore generator using swagger codegen
How to make an almost static page with rails
Try to make an addition program in several languages
[Rails 6] How to set a background image in Rails [CSS]
How to write an external reference key in FactoryBot
How to create an application
Java --How to make JTable
How to handle an instance
[Rails] How to make seed
I tried to make an application in 3 months from inexperienced
How to pass an object to Mapper in MyBatis without arguments
How to change a string in an array to a number in Ruby
[Rails] How to define macros in Rspec and standardize processing
How to retrieve the hash value in an array in Ruby
What happened in "Java 8 to Java 11" and how to build an environment
How to make an app using Tensorflow with Android Studio
What is an immutable object? [Explanation of how to make]
[Ruby] How to batch convert strings in an array to numbers
How to use Lombok in Spring
How to find May'n in XPath
[Ruby] How to count even or odd numbers in an array
Processing to issue an error message
Make an FPS counter in Swift
How to hide scrollbars in WebView
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to run Ant in Gradle
How to make a jar file with no dependencies in Maven
How to make a Java container
How to master programming in 3 months
How to make a JDBC driver
How to learn JAVA in 7 days
How to get parameters in Spark
How to output the value when there is an array in the array
[Processing × Java] How to use variables
How to install Bootstrap in Ruby
How to insert an external library
How to use InjectorHolder in OpenAM
How to make a splash screen
How to make a Jenkins plugin
How to introduce jQuery in Rails 6
How to use classes in Java?
How to make a Maven project
How to name variables in Java