How to Convert Colored Image in Java?

This article dives deep into the Read and Write Operation in Image Processing in Java and how to convert colored image to greyscale, negative, Red Green Blue, and Sepia image in java. Let’s start!!!

Image Processing in Java – Read and Write:

The implementation of the BufferedImage object in Java is vital for the advancement of Image Processing. It can be read from various distinct types of images. These image types include BMP, HEIC, etc.

Some of the image types are backed by ImageIO, but not all. In such cases, the presence of plugins helps in extending the ImageIO and other libraries like Apache Imaging.

In Java, the complexity of the image types is hidden. We easily work on BufferedImage without any difficulties. Java also provides access to the image pixels and color information and allows image conversion and processing.

Classes required to perform Java read and write operations:

1. java.io.File: It helps to read and write an image file. It requires the importing of the File class. And this class contains the name of the file and directory path.

2. java.io.IOException: It helps to handle errors, we must use the IOException class.

3. java.awt.image.BufferedImage: To hold the image, we create the BufferedImage object; we use BufferedImage class. This object is used to store in RAM.

4. javax.imageio.ImageIO: To perform the image read-write operation, we must the ImageIO class. This class contains static methods to read and write an image.

Sample program to demonstrate read and write of image:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
  
public class NewImage {
public static void main(String args[])
throws IOException
{
int width = 783;
int height = 550;
BufferedImage image = null;
try {
File input_file = new File(
"C:/Users/hp/Desktop/Image Processing in Java/Input.png");

/* image file path create an object of
BufferedImage type and pass as parameters the
width,  height, and image int
type. TYPE_INT_ARGB means that we are
representing the Alpha, Red, Green, and Blue
components of the image pixel using 8-bit
integer value. */

image = new BufferedImage(
width, height, BufferedImage.TYPE_INT_ARGB);
image = ImageIO.read(input_file);
System.out.println("Reading complete.");
}
catch (IOException e) {
System.out.println("Error: " + e);
}
try {
File output_file = new File("C:/Users/hp/Desktop/Image Processing in Java/Input.png");
ImageIO.write(image, "png", output_file);
System.out.println("Writing is complete");
}
catch (IOException e) {
System.out.println("Error: " + e);
}
} 
} 

Output:

Video Player

Conversion of the coloured image in java:

Image processing plays a vital role to convert the colored image in:

  • Grayscale
  • Red
  • Green
  • Blue
  • Sepia image and
  • Negative image

Conversion of Colored image to Grayscale image in Java:

The following algorithm helps in converting a colored image to a grayscale image:

1: Firstly, get the RGB value of the pixel using the getRGB() method.

2: Attain the average of the RGB with the formula, average = (R +G+B) /3

3: Now, apply the calculated average value in the place of R, G, B values of the pixel.

4: Repeat Step 1 to Step 3 for each pixel of the image.

Program code to convert a colored image to a grayscale image:

import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class FirstCodeGreyscale
{
public static void main(String args[])throws IOException
{
BufferedImage image = null;
File file = null;
try
{
file = new File("/home/Desktop/Input.jpeg");
image = ImageIO.read(file);
}
catch(IOException e)
{
System.out.println(e);
}
int width = image.getWidth();
int height = image.getHeight();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int p = image.getRGB(x,y);
int a = (p>>24) & 0xff;
int r = (p>>16) & 0xff;
int g = (p>>8) & 0xff;
int b = p & 0xff;
int avg = (r+g+b)/3;
p = (a<<24) | (avg<<16) | (avg<<8) | avg;
image.setRGB(x, y, p);
}
}
try
{
file = new File("/home/Desktop/greyscale.png");
ImageIO.write(image, "png", file);
System.out.println("Successfully converted a colored image into a grayscale image");
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Output:

Successfully converted a colored image into a grayscale image

Conversion of Colored image to Negative Image in Java:

The algorithm to convert a colored image to a negative image is given below:

1: Get the RGB value of the pixel by applying the getRGB() method.

2: Now, calculate the RGB values as given below:

  • R = 255 – R
  • G = 255 – G
  • B = 255 – B

3: Apply the previously calculated value in the place of R, G, B of the pixels.

4: Repeat Steps 1 to 3 for every pixel present in the image.

Program code to convert a colored image to a negative image:

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class FirstCodeNegative
{
public static void main(String args[])throws IOException
{
BufferedImage image = null;
File file = null;
try
{
file = new File("/home/Desktop/Input.jpeg");
image = ImageIO.read(file);
}
catch(IOException e)
{
System.out.println(e);
}
int width = image.getWidth();
int height = image.getHeight();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int p = image.getRGB(x,y);
int a = (p>>24) & 0xff;
int r = (p>>16) & 0xff;
int g = (p>>8) & 0xff;
int b = p & 0xff;
r = 255 - r;
g = 255 - g;
b = 255 - b;
p = (a<<24) | (r<<16) | (g<<8) | b;
image.setRGB(x, y, p);
}
}
try
{
file = new File("/home/Desktop/negative.jpeg");
ImageIO.write(image, "jpg", file);
System.out.println("Successfully converted a colored image into a negative image");
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Output:

Successfully converted a colored image into a negative image

Conversion of a colored image to Red Green Blue Image in Java:

Here is the algorithm to convert a colored image to a red-colored image in Java:

Step 1: Get the RGB value of the pixel to a red-colored image.

Step 2: Now, set the values of R, G, and B as given below:

  • R: No Change
  • G: Set to 0
  • B: Set to 0

Step 3: Apply the calculated value in the place of R, G, and B values.

Step 4: Repeat steps 1 to 3 for every pixel of the image.

Program code to convert a colored image to a red-colored image:

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class FirstCodeRedImage
{
public static void main(String args[])throws IOException
{
BufferedImage image = null;
File file = null;
try
{
file = new File("/home/Desktop/Input.jpeg");
img = ImageIO.read(f);
}
catch(IOException e)
{
System.out.println(e);
}
int width = image.getWidth();
int height = image.getHeight();
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;
p = (a<<24) | (r<<16) | (0<<8) | 0;
image.setRGB(x, y, p);
}
}
try
{
file = new File("/home/Desktop/red.jpeg");
System.out.println("Successfully converted a colored image into a red image");
ImageIO.write(image, "jpeg", file);
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Output:

Successfully converted a colored image into a red image

Conversion of a Colored image to Sepia image in Java:

Here is the algorithm to convert a colored image to a Sepia image:

1: Get the RGB value of the pixel by applying the getRGB() method.

2: Calculate new values of R, G, and B of the pixel.

3: Set the new values of R, G, and B values of the pixel according to the below conditions:

  • If newRed value > 255, then set R = 255 else set R = newRed
  • If newGreen value > 255, then set G = 255 else set G = newGreen
  • When newBlue value > 255, then set B = 255 else set B = newBlue

4: Apply the new value in the place of R, G, and B values.

5: Repeat Steps 1 to 4 for every pixel of the image.

Program code to convert a colored image to a sepia image:

import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class FirstCodeSepia
{
public static void main(String args[])throws IOException
{
BufferedImage image = null;
File file = null;
try
{
file = new File("/home/Desktop/Input.jpeg");
image = ImageIO.read(file);
}
catch(IOException e)
{
System.out.println(e);
}
int width = image.getWidth();
int height = image.getHeight();
//converting 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;
int newR = (int)(0.393*R + 0.769*G + 0.189*B);
int newG = (int)(0.349*R + 0.686*G + 0.168*B);
int newB = (int)(0.272*R + 0.534*G + 0.131*B);
if (newR > 255)
R = 255;
else
R = newR;
if (newG > 255)
G = 255;
else
G = newG;
if (newB > 255)
B = 255;
else
B = newB;
p = (a<<24) | (R<<16) | (G<<8) | B;
image.setRGB(x, y, p);
}
}
try
{
file = new File("/home/Desktop/sepia.jpeg");
System.out.println("Successfully converted a colored image into a sepia image");
ImageIO.write(image, "jpg", file);
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Output:

Successfully converted a colored image into a sepia image

Conclusion

Now we have seen the conversion of a colored image into the various form using Image processing in Java. You can apply them to make the desired modifications in your output. Also, refer to the Image Processing Part 1 to know about various other techniques that come under Image processing.

Leave a Reply

Your email address will not be published. Required fields are marked *