Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8617971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:59:56+00:00 2026-06-12T05:59:56+00:00

I want to overlap a black image containing some white spots on a grey

  • 0

I want to overlap a black image containing some white spots on a grey shade image.I want to make this black image transparent. I used the code mentioned here in Java.

But the output shows a red image. Why?

The code is mentioned here :

    public class OverlayImage{
        public static void main(String[] args) {
            File path = new File("/home/dmdd/Desktop/images"); // base path of the images

            // load source images
            BufferedImage image=null;
            BufferedImage overlay=null;
            try {
                image = ImageIO.read(new File(path, "image.jpg"));
                overlay = ImageIO.read(new File(path, "overlay.jpg"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //overlay =(BufferedImage) makeImageTransparent(overlay,50);
            OverlayImage over=new OverlayImage();
            // 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);

            Image overlay1=over.TransformGrayToTransparency(overlay);
            BufferedImage combined=over.ApplyTransparency(image, overlay1);
            try {
                ImageIO.write(over.toBufferedImage(combined, w, h), "JPG", new File(path, "combined.jpg"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        private Image TransformGrayToTransparency(BufferedImage image)
        {
            ImageFilter filter = new RGBImageFilter()
            {
                public final int filterRGB(int x, int y, int rgb)
                {
                    return (rgb << 8) & 0xFF000000;
                }
            };

            ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
            return Toolkit.getDefaultToolkit().createImage(ip);
        }

        private BufferedImage ApplyTransparency(BufferedImage image, Image mask)
        {
            BufferedImage dest = new BufferedImage(
                    image.getWidth(), image.getHeight(),
                    BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = dest.createGraphics();
            g2.drawImage(image, 0, 0, null);
            AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.DST_IN, 1.0F);
            g2.setComposite(ac);
            g2.drawImage(mask, 0, 0, null);
            g2.dispose();
            return dest;
        }
}

The white spot image is:
enter image description here

The black image is:
enter image description here

The overlayed red image is:
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:59:57+00:00Added an answer on June 12, 2026 at 5:59 am

    You are trying to save the resulting image as a jpeg. The resulting image contains the alpha / transparency level, while jpeg images don’t support transparency. Save it as a png image.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want the black and white squares that share a tile/cell to overlap. The
I have this html file where I want to overlay some text over another.
I want overlap the border-bottom of <span> above the border-bottom of <h1> . In
I have two UILabels. that I want to overlap one atop the other. Call
I want to know if there is any overlap between polygon1.the_geom and polygon2.the_geom .
I have a tiling background image, but I want to overlay another tiling image
Ok so I have a Background background-image: url('images/body.png'); now I want to overlay a
I have a high-resolution image that I want to use as a tiled map
I am a complete novice to image processing, and I am guessing this is
I'm trying to draw an image with a certain color. To get this effect,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.