0

How can I remove white background color(white in this) from image?. Or Want to make image background transparent.

enter image description here

These are two images.
Image 1 is: Text with white background
Image 2 is: Green(or close to green color) image

I need to make white remove background transparent of image 1

Ali
  • 3,545
  • 11
  • 44
  • 63

4 Answers4

0

Here is an example to make BLUE color transparent in this image. enter image description here

Here we have an Image with a blue background like and we want to display it in an Applet with a white background. All we have to do is to look for the blue color with the "Alpha bits" set to opaque and make them transparent.

You can adapt it and make WHITE color transparent.

Also you can check this answer.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 1
    this is not a full answer, this try to guide OP to goal... as far as there is no code provided, how can you be sure process converting image will be in android? maybe the image only has to be converted once... Also, is not impossible to convert this function from `java.awt` to `android.graphics`. Last but not least... feel free to post your own answer so we can help OP further :) – Jordi Castilla Sep 07 '15 at 10:53
  • 1
    you are very right, the question does not provide many clues - i did transparency with like described in your answer - worked fine for me... – Martin Frank Sep 07 '15 at 10:55
  • 2
    excuse me, when i was sounding harsh - i didn't meant to do so... sometimes that happens and i'm really sorry for that :-$ – Martin Frank Sep 07 '15 at 10:58
  • puh - when i did this, that was looooooong ago, i barely can remeber what i did then - the idea is to read an image into an buffer and then just walk over that buffer and edit each pixel... afterwards create an file with your buffer... – Martin Frank Sep 07 '15 at 11:26
  • 1
    cool that you are a tolerant man - i hope i'll be the same sometimes... (still working on it)... – Martin Frank Sep 07 '15 at 11:31
0

The question is that, you want to make background transparent at runtime or just this image? if just this one then you should edit the image with any tool like irfanView to remove background color. just open this image in this tool and then save it as PNG and tick save transparent color and untick use window background color and then press enter. After that select white color.

Bilal Haider
  • 493
  • 4
  • 22
0

You can do it easily with photoshop.

Using the magic wand or lasso tool, select the area of the image you want to be transparent. enter image description here

and then just hit DELETE button.

enter image description here

for additional information check this link

you can also check this tutorial

here is useful video

hornet2319
  • 379
  • 3
  • 15
  • Thanks for answer. I want to make background transparent at runtime. I am rendering lot of images in android app. I am adding this in container at runtime. How can I do this pragmatically? – Ali Sep 07 '15 at 11:08
  • @smoke sorry, but I dont know how to do similar things in runtime =/ – hornet2319 Sep 07 '15 at 11:12
0

when i had to solve that problem i used a buffer...

private IntBuffer buffer;
public void toBuffer(File tempFile){
    final Bitmap temp = BitmapFactory.decodeFile(imgSource
            .getAbsolutePath());
    buffer.rewind(); //setting the buffers index to '0'
    temp.copyPixelsToBuffer(buffer);
}

then you can simply edit all pixels (int-values) on your buffer (as mentioned from https://stackoverflow.com/users/3850595/jordi-castilla) ... ...setting ARGB for 0xFFFFFFFF (white&transparent) to 0x00?????? (any color would suit, it's transparent anyway)

so here's your code to edit transparenty on a buffer:

public void convert(){
    for (int dy = 0; dy < imgHeight; dy ++){            
        for (int dx = 0; dx < imgWidth; dx ++ ){

            int px = buffer.get();
            int a = (0xFF000000 & px) >> 24;
            int r = (0x00FF0000 & px) >> 16;
            int g = (0x0000FF00 & px) >> 8;
            int b = (0x000000FF & px);

            int result = px;    

            if (px == 0xFFFFFFFF){ //only adjust alpha when the color is 'white'
               a = 0xFF000000; //transparent? 
               int result = a | r | g | b;
            }

            int pos = buffer.position();
            buffer.put(pos-1, result);
        }       
    }

later you want to write back your image into an converted file:

public void copyBufferIntoImage(File tempFile) throws IOException {
    buffer.rewind();
    Bitmap temp = Bitmap.createBitmap(imgWidth, imgHeight,
            Config.ARGB_8888);
    temp.copyPixelsFromBuffer(buffer);

    FileOutputStream out = new FileOutputStream(tempFile);
    temp.compress(Bitmap.CompressFormat.PNG, 90, out);
    out.flush();
    out.close();
}

maybe you still want to know how to map a buffer?

    public void mapBuffer(final File tempFile, long size) throws IOException {

    RandomAccessFile aFile = new RandomAccessFile(tempFile, "rw");
    aFile.setLength(4 * size); // 4 byte pro int
    FileChannel fc = aFile.getChannel();
    buffer = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size())
            .asIntBuffer();

}
Community
  • 1
  • 1
Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • maybe i'm mixing up the color model, i can't remember if 100% opaque=`0xFF??????`or 100%opaque=`0x00??????` – Martin Frank Sep 07 '15 at 11:30
  • so you have to edit the color values for yourself - maybe you want to use any other color as transparent... i'm pretty sure coloring isn't your actual problem ^^ – Martin Frank Sep 07 '15 at 11:34