I need my program to go through the pixels in an image, change them to grey scale. Then I need to take a range of gray values and colorize them using if - else and if-else-if statements. Can someone please help me figure this out?
Here's my code so far:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Colorize {
BufferedImage image;
int width;
int height;
public Colorize() {
try {
File input = new File("Grayscale.jpg");
image = ImageIO.read(input);
width = image.getWidth();
height = image.getHeight();
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
int col = image.getRGB(i, j);
Color c = new Color(col, true);
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
if ((red>= 1)&&(red<=30)) {
c = new Color(c.getRed() + 10, c.getGreen(), c.getBlue());
}
if ((red>= 31)&&(red<=60)) {
c = new Color(c.getRed(), c.getGreen() + 10, c.getBlue());
}
if ((red>= 61)&&(red<=90)) {
c = new Color(c.getRed(), c.getGreen(), c.getBlue() + 10);
}
if ((red>= 91)&&(red<=120)) {
c = new Color(c.getRed() + 10, c.getGreen() + 10, c.getBlue());
}
if ((red>= 121)&&(red<=150)) {
c = new Color(c.getRed() + 10, c.getGreen(), c.getBlue() + 10);
}
if ((red>= 151)&&(red<=180)) {
c = new Color(c.getRed(), c.getGreen() + 10, c.getBlue() + 10);
}
if ((red>= 181)&&(red<=210)) {
c = new Color(c.getRed() - 10, c.getGreen(), c.getBlue());
}
if ((red>= 211)&&(red<=240)) {
c = new Color(c.getRed(), c.getGreen() - 10, c.getBlue());
}
else {
c = new Color(c.getRed(), c.getGreen(), c.getBlue());
}
image.setRGB(j,i,c.getRGB());
}
}
File output = new File("Colorize.jpg");
ImageIO.write(image, "jpg", output);
} catch (Exception e) {}
}
static public void main(String args[]) throws Exception
{
Colorize obj = new Colorize();
}
}
Here's the image in case you guys want to try the code out. So far nothing is being written to the folder.