Hello All,

Just looking for code snippets to remove color from jpg image.

I would like to remove red, blue, green from a picture, leaving black and white. For instance, imagine a newspaper cartoon with an "X" on the picture. I want to remove the colors leaving the "X". I'd also like to have Jscrollpanel show with original image and one with new image next to it (two JScrollPanes on same screen) so I can see what is happening ans I progress.

Thanks in advance.

Dani AI

Generated

For : a simple, reliable approach is to detect and keep dark / low‑saturation pixels (the black ink) and turn everything else to white. As pointed toward grayscale/thresholding, the variant below uses HSB so highly saturated colors (red, blue, green fills) become white while black/gray strokes stay black. Tweak the saturation/brightness thresholds to match your images.

import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;

public class RemoveColor {
    public static BufferedImage extractBlack(BufferedImage src, float satThresh, float brightThresh) {
        int w = src.getWidth(), h = src.getHeight();
        BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                int rgb = src.getRGB(x, y);
                int r = (rgb >> 16) & 0xff, g = (rgb >> 8) & 0xff, b = rgb & 0xff;
                float[] hsb = Color.RGBtoHSB(r, g, b, null);
                float sat = hsb[1], bri = hsb[2];
                if (bri < brightThresh || (sat < satThresh && bri < 0.9f)) {
                    out.setRGB(x, y, 0xFF000000); // black
                } else {
                    out.setRGB(x, y, 0xFFFFFFFF); // white
                }
            }
        }
        return out;
    }

    public static void main(String[] args) throws Exception {
        BufferedImage src = ImageIO.read(new File("input.jpg"));
        BufferedImage processed = extractBlack(src, 0.4f, 0.6f);
        SwingUtilities.invokeLater(() -> {
            JFrame f = new JFrame("Original / Processed");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JLabel a = new JLabel(new ImageIcon(src));
            JLabel b = new JLabel(new ImageIcon(processed));
            JScrollPane sp1 = new JScrollPane(a);
            JScrollPane sp2 = new JScrollPane(b);
            JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sp1, sp2);
            split.setResizeWeight(0.5);
            f.add(split);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        });
    }
}

Notes and tweaks:

  • Start with satThresh ≈ 0.3–0.5 and brightThresh ≈ 0.55–0.7 and adjust to your images.
  • For large images, read/write rows with getRGB(..., int[] ...) for speed.
  • If small colored specks remain, apply a median filter or simple morphological ops (dilate/erode) to the binary mask; libraries such as BoofCV/Marvin can help.
  • Save final output as PNG to avoid additional JPEG compression artifacts.
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.