Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Image Processing in Java – Colored Image to Sepia Image Conversion

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Prerequisites: 

In this set, we will be converting a colored image to a sepia image. In a sepia image, the Alpha component of the image will be the same as the original image(since the alpha component denotes the transparency). Still, the RGB will be changed, which will be calculated by the following formula. 

newRed = 0.393*R + 0.769*G + 0.189*B
newGreen = 0.349*R + 0.686*G + 0.168*B
newBlue = 0.272*R + 0.534*G + 0.131*B

If any of these output values is greater than 255, simply set it to 255. These specific values are the values for a sepia tone that are recommended by Microsoft.

Algorithm: 

  1. Get the RGB value of the pixel.
  2. Calculate newRed, new green, newBlue using the above formula (Take the integer value)
  3. Set the new RGB value of the pixel as per the following condition: 
    • If newRed > 255 then R = 255 else R = newRed
    • If newGreen > 255 then G = 255 else G = newGreen
    • If newBlue > 255 then B = 255 else B = newBlue
  4. Replace the value of R, G, and B with the new value that we calculated for the pixel.
  5. Repeat Step 1 to Step 4 for each pixel of the image.

Implementation:

Java




// Java program to demonstrate
// colored to sepia conversion
  
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
  
public class Sepia {
    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 = ImageIO.read(f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
  
        // get width and height of the image
        int width = img.getWidth();
        int height = img.getHeight();
  
        // convert to sepia
        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;
  
                // calculate newRed, newGreen, newBlue
                int newRed = (int)(0.393 * R + 0.769 * G
                                   + 0.189 * B);
                int newGreen = (int)(0.349 * R + 0.686 * G
                                     + 0.168 * B);
                int newBlue = (int)(0.272 * R + 0.534 * G
                                    + 0.131 * B);
  
                // check condition
                if (newRed > 255)
                    R = 255;
                else
                    R = newRed;
  
                if (newGreen > 255)
                    G = 255;
                else
                    G = newGreen;
  
                if (newBlue > 255)
                    B = 255;
                else
                    B = newBlue;
  
                // 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) {
            System.out.println(e);
        }
    }
}


Output:

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

This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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.


My Personal Notes arrow_drop_up
Last Updated : 14 Nov, 2021
Like Article
Save Article
Similar Reads
Related Tutorials