Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two images

I need to merge two images (BufferedImage) in Java. It wouldn't be a problem if there was no transparency. The base image already has some transparency. I want to keep this as it is and apply a "mask" to it, the second image. This second image has no opaque pixels, in fact it's almost completely transparent, just has some less transparent pixels to give some sort of "light effect", like a reflex. Important detail: I don't want to do this on screen, with graphics, I need to obtain a BufferedImage with the resultant merge.

Can anyone help me? Thanks!

DETAILS: Merge two images maintaining transparency. This is what I need to do.

Note: this Set BufferedImage alpha mask in Java does not do what I need because it does not handle well with the two images having transparency - it modifies first picture transparency.

like image 552
GuilhermeA Avatar asked Feb 23 '10 12:02

GuilhermeA


People also ask

Is there a free app to merge two pictures together?

Use Pic Stitch to create a before-and-after sequence, combine great photos into a collage, or produce a photographic series. Quickly combine multiple photos and videos into one beautifully framed picture. Then share your masterpiece on your favorite social media networks including Facebook, Twitter and Instagram.

What is merging two photos called?

Photomontage is the process and the result of making a composite photograph by cutting, gluing, rearranging and overlapping two or more photographs into a new image. Sometimes the resulting composite image is photographed so that the final image may appear as a seamless physical print.


2 Answers

Just create a new BufferedImage with transparency, then paint the other two images (with full or semi-transparency) on it. This is how it will look like:

Image plus overlay

Sample code (images are called 'image.png' and 'overlay.png'):

File path = ... // base path of the images  // load source images BufferedImage image = ImageIO.read(new File(path, "image.png")); BufferedImage overlay = ImageIO.read(new File(path, "overlay.png"));  // create the new image, canvas size is the max. of both image sizes int w = Math.max(image.getWidth(), overlay.getWidth()); int h = Math.max(image.getHeight(), overlay.getHeight()); BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);  // paint both images, preserving the alpha channels Graphics g = combined.getGraphics(); g.drawImage(image, 0, 0, null); g.drawImage(overlay, 0, 0, null);  g.dispose();  // Save as new image ImageIO.write(combined, "PNG", new File(path, "combined.png")); 
like image 174
Peter Walser Avatar answered Oct 23 '22 23:10

Peter Walser


I can't give you a specific answer, but java.awt.AlphaComposite here is your friend. You'll get absolute control over how you want the two images to merge. However it is not straightforward to use - you need to learn a bit of graphics theory first.

like image 31
Steve McLeod Avatar answered Oct 24 '22 00:10

Steve McLeod