Ewen W.
Ewen W.

Reputation: 199

How to cut out a polygon shaped part of an image?

I have a polygon with the following vertices...

20,20
20,30
40,30

and an image. I want to cut the polygon shaped part out of the image to form a new one. Is there any algorithm to do so? I am using Processing if that's anything helpful.

Thanks in advance.

Upvotes: 4

Views: 2292

Answers (1)

ug_
ug_

Reputation: 11440

You could create a new image and paint the old one on with the clip set.

BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);

Graphics g = image.getGraphics();

Path2D path = new Path2D.Double();
path.moveTo(20, 20);
path.lineTo(20, 30);
path.lineTo(40, 30);
path.closePath();

g.setClip(path);
g.drawImage(YourOrigioanlImage, 0, 0, null);

It looks like in "Processing" you can create a PImage from an java.awt.Image so you can get your PImage by using the code sample above then doing this:

PImage pImage = new PImage(image);

You can also get a BufferedImage object from a PImage by casting the getNative() method from PImage

Upvotes: 4

Related Questions