swapnil gandhi swapnil gandhi - 4 months ago 9
Java Question

changing image color in java

I am trying to change color of image. Hence I used the following code

public class Picture {

String img_name;
BufferedImage buf_img;
int width;
int height;

public Picture(String name) {
this.img_name = name;

try {
buf_img = ImageIO.read(new File(img_name));
} catch (IOException ex) {
Logger.getLogger(Picture.class.getName()).log(Level.SEVERE, null, ex);
}
}

public Picture(int w, int h) {
this.width = w;
this.height = h;
buf_img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}

public int width() {
width = buf_img.getWidth();
return width;
}

public int height() {
height = buf_img.getHeight();
return height;
}

public Color get(int col, int row) {
Color color = new Color(buf_img.getRGB(col, row));
return color;
}

public void set(int col, int row, Color color) {
buf_img.setRGB(col, row, color.getRGB());
}

public void show() {
try {

File saveAs = new File("D:\\temp\\" + new Random().nextInt() + ".png");
ImageIO.write(buf_img, "png", saveAs);

Desktop.getDesktop().open(saveAs);
} catch (IOException ex) {
Logger.getLogger(Picture.class.getName()).log(Level.SEVERE, null, ex);
}
}

}

public class ColorSeparation {

public static void main(String[] args) {

// read in the picture specified by command-line argument
Picture picture = new Picture("D:\\qwe1.jpg");
int width = picture.width();
int height = picture.height();

// create three empy pictures of the same dimension
Picture pictureR = new Picture(width, height);


// separate colors
for (int col = 0; col < width; col++) {
for (int row = 0; row < height; row++) {
Color color = picture.get(col, row);
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
pictureR.set(col, row, new Color(r, 0, 0));

}
}

// display picture in its own window
pictureR.show();

}

}


It is working as expected. input Image

Output image.

Now I want to set color of entire image to rgb 255, 153, 51. I tried to set
pictureR.set(col, row, new Color(255, 153, 51))
. But the resulted output is the below imageresulting image.

How can I get correct image? Please help.

Answer

Your initial example is misleading you. Your code in the first example is setting varying shades of red (pulled from the original red channel), creating a "redscale" image, not "colorizing" the image like you think it is.

int r = color.getRed();
pictureR.set(col, row, new Color(r, 0, 0));

Your second example is setting a fixed color for each and every pixel, so you get a uniform orange.

pictureR.set(col, row, new Color(255, 153, 51))

You need to colorize the image by varying all three channels, just like you varied the red value initially. See this question for an example using compositing, which is a different angle from what you're using now.

The easiest implementation for you, given your sample code, would be to calculate the relative luminence of each pixel (effectively it's grayscale value) and use that to adjust the "orange" value you are setting. The standard weighting for luminence is

L = 0.2126*R + 0.7152*G + 0.0722*B

so something like

pictureR.set(col, row, new Color(255 * L/255, 153 * L/255, 51 * L/255));