Convert jpg to png with background transparency

When converting from jpg to png background transparency image, i don't want the dotted border on the converted image

My Original Image (JPG) My original image

My converted image (PNG)

enter image description here

My source code

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;

import javax.imageio.ImageIO;

public class DemoTransparent {

    public static void main(String[] args) throws Exception {

        URL url = new URL("http://i.stack.imgur.com/jEqbx.jpg");
        Image  image1 = ImageIO.read(url);
        BufferedImage source = (BufferedImage) image1;
        int color = source.getRGB(0, 0);
        Image image = makeColorTransparent(source, new Color(color));
        BufferedImage transparent = imageToBufferedImage(image);
        File out = new File("D:\\Demo.png");
        ImageIO.write(transparent, "PNG", out);

    }

    private static BufferedImage imageToBufferedImage(Image image) {

        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR_PRE);

        Graphics2D g2 = bufferedImage.createGraphics();
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
        return bufferedImage;

    }

    public static Image makeColorTransparent(BufferedImage im, final Color color) {
        ImageFilter filter = new RGBImageFilter() {

            public int markerRGB = color.getRGB() | 0xFF000000;
            public final int filterRGB(int x, int y, int rgb) {
                if ((rgb | 0xFF000000) == markerRGB) {
                    return 0x00FFFFFF & rgb;
                } else {
                    return rgb;
                }
            }


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

}

I want only transparency background PNG Image

+4
source share
1 answer

Usually we do to extract a color that is exactly equal to or almost equal to the color, takes a threshold.

Here is your code with a few changes:

// Take another parameter i.e. threshold
public static Image makeColorTransparent(BufferedImage im, final Color color, float threshold) {
    ImageFilter filter = new RGBImageFilter() {
        public float markerAlpha = color.getRGB() | 0xFF000000;
        public final int filterRGB(int x, int y, int rgb) {
            int currentAlpha = rgb | 0xFF000000;           // just to make it clear, stored the value in new variable
            float diff = Math.abs((currentAlpha - markerAlpha) / markerAlpha);  // Now get the difference
            if (diff <= threshold) {                      // Then compare that threshold value
                return 0x00FFFFFF & rgb;
            } else {
                return rgb;
            }
        }
    };
    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}

makeColorTransparent(image, color, 0.05f);
. ( ) . , algo .

, , . . , . 0.05f , , ( Photoshop ).

enter image description here

+6

Source: https://habr.com/ru/post/1614827/


All Articles