I imported a picture onto another picture and I am wondering how to get rid of the white around the smaller picture.

Dani AI

Generated

The white “halo” occurs because the overlay image’s white pixels are opaque rather than transparent. The simplest real-world fix is to use an image that already has an alpha channel (a PNG or a GIF saved with a transparent index). When that is not possible, two robust in-code options are common: a lightweight AWT filter that makes a keyed color transparent, or converting the image to a BufferedImage and clearing pixels (with a tolerance) to produce true ARGB transparency. was right to point toward a BufferedImage-based approach; the examples below show both patterns and a note about the backbuffer issue seen in the posted code.

Quick AWT filter (runtime color-keying)

Image src = Toolkit.getDefaultToolkit().getImage("bug1right.gif"); // or getImage(...)
ImageFilter filter = new RGBImageFilter() {
    private final int marker = (Color.WHITE.getRGB() | 0xFF000000);
    public int filterRGB(int x, int y, int rgb) {
        if ((rgb | 0xFF000000) == marker) return 0; // fully transparent
        return rgb;
    }
};
Image transparent = Toolkit.getDefaultToolkit()
    .createImage(new FilteredImageSource(src.getSource(), filter));

BufferedImage (pixel-accurate, recommended for repeated drawing)

BufferedImage src = ImageIO.read(new File("bug1right.gif")); // or use getResourceAsStream()
int w = src.getWidth(), h = src.getHeight();
BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
int tol = 240; // threshold for "near-white"
for (int y=0; y<h; y++){
  for (int x=0; x<w; x++){
    int p = src.getRGB(x,y);
    int r = (p>>16)&0xFF, g=(p>>8)&0xFF, b=p&0xFF;
    if (r>tol && g>tol && b>tol) out.setRGB(x,y, p & 0x00FFFFFF); // alpha=0
    else out.setRGB(x,y, 0xFF000000 | (p & 0x00FFFFFF)); // opaque
  }
}

Backbuffer and loading notes:

  • Do not overwrite a loaded background image with createImage(...). Keep a separate bgImage, create the backbuffer with the component size (create lazily in paint when getWidth()/getHeight() are valid), draw bgImage into backbuffer, then draw the prepared transparent overlay onto the backbuffer and finally draw the backbuffer to the screen.
  • Use ImageIO.read or MediaTracker before pixel inspection to ensure images are fully loaded.
  • Alpha semantics: 0 means fully transparent, 255 (0xFF) fully opaque — set values accordingly.

These approaches avoid manual clipping of rectangular frames and give control over tolerance and performance. ’s BufferedImage idea is the most flexible; the RGBImageFilter is handy for quick keying without pixel loops. ’s note that the problem was resolved is expected once one of these fixes is applied.

Recommended Answers

All 4 Replies

We'll need mode details
How do you import a picture into another picture ?

I didn't import the picture INTO the picture I just imported it onto the picture. Anyway here is my script right now. Also I cannot seem to get the backbuffer working.

// The "GreenScreen" class.
import java.applet.*;
import java.awt.*;

public class GreenScreen extends Applet
{
    Image img, backbuffer;
    Graphics backg;

    // Place instance variables here

    public void init ()
    {

        //img = getImage (getDocumentBase(), "bug1right.jpg");
        //backg = getImage (getDocumentBase(), "hedge.jpg");
        img = getImage (getDocumentBase (), "bug1right.gif");
        backbuffer = getImage (getDocumentBase (), "hedgelarge.gif");

        backbuffer = createImage (100, 100);
        backg = backbuffer.getGraphics ();
        backg.setColor (Color.white);
        // Place the body of the initialization method here
    } // init method


    public void paint (Graphics g)
    {
        g.drawImage (backbuffer, 0, 0, this);
        g.drawImage (img, 10, 10, 50, 50, this);
        // Place the body of the drawing method here
    } // paint method
} // GreenScreen class

And I presume that bug1right.gif has a white frame ?

Not an easy task.

If it is the case, not an easy task, you can make a BufferedImage out of it
Then by getRgb() method you can get an double dimension array of all the pixels in your image. The array will be a int[imageWidth][imageHeight]
You can then make 2 nested loop to inspect the pixels on the border. If the color is +/- white (something like red > 240, green > 240, blue > 240) you can set the alpha to 255 for these pixels.
Then you can rebuild and Image of TYPE_4BYTES_ABGR (Alpha, Reg, Green, Blue)

If the frame if perfectly rectangular you can just search where the color change from left to right and top to bottom and just draw the subImage

I figured out how to fix it. thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.