Open In App
Related Articles

Image Processing in Java – Colored image to Negative Image Conversion

Improve Article
Save Article
Like Article


In this set, we will be converting a colored image to a negative image. 

Colored Image (RGB Color Model) – The RGB color model is an additive mixing model in which red, green, and blue light are added together in various ways to reproduce a broad array of colors. 

Negative Image – A negative image is a total inversion, in which light areas appear dark and vice versa. A negative color image is additionally color-reversed, with red areas appearing cyan, greens appearing magenta, and blues appearing yellow, and vice versa.

Image negative is produced by subtracting each pixel from the maximum intensity value. For example in an 8-bit grayscale image, the max intensity value is 255, thus each pixel is subtracted from 255 to produce the output image.

Note: In a negative image the Alpha component of the image will be the same as the original image, but the RGB will be changed i.e, all three RGB components will be having a value of 255-original component value.


  1. Get the RGB value of the pixel.
  2. Calculate new RGB values as follows: 
    • R = 255 – R
    • G = 255 – G
    • B = 255 – B
  3. Replace the R, G, and B values of the pixel with the values calculated in step 2.
  4. Repeat Step 1 to Step 3 for each pixel of the image.



// Java program to demonstrate
// colored to negative conversion
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Negative {
    public static void main(String args[])
        throws IOException
        BufferedImage img = null;
        File f = null;
        // read image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
            img =;
        catch (IOException e) {
        // Get image width and height
        int width = img.getWidth();
        int height = img.getHeight();
        // Convert to negative
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int p = img.getRGB(x, y);
                int a = (p >> 24) & 0xff;
                int r = (p >> 16) & 0xff;
                int g = (p >> 8) & 0xff;
                int b = p & 0xff;
                // subtract RGB from 255
                r = 255 - r;
                g = 255 - g;
                b = 255 - b;
                // set new RGB value
                p = (a << 24) | (r << 16) | (g << 8) | b;
                img.setRGB(x, y, p);
        // write image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/GFG.png");
            ImageIO.write(img, "png", f);
        catch (IOException e) {

Output – 

Note: This code will not run on online IDE as it needs an image on disk. 

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 14 Nov, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials