Reputation: 395
Following is the code I have written to change the pixel values of a gray scale image to create a full white image (all pixel values set to 255). But instead of getting a complete white image I'm getting the black vertical bars in between as shown in the figure 2. Where have I gone wrong?
package dct;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
public class writeGScale {
public static void main(String[] args){
File file = new File("bridge.jpg");
BufferedImage img = null;
try{
img = ImageIO.read(file);
}
catch(Exception e){
e.printStackTrace();
}
int width = img.getWidth();
int height = img.getHeight();
int[] tempArr = new int[width*height];
WritableRaster raster1=img.getRaster();
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
tempArr[0] = 255;
raster1.setPixel(i, j, tempArr);
}
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
image.setData(raster1);
try{
File ouptut = new File("change.png");
ImageIO.write(image, "png", ouptut);
}
catch(Exception e){
e.printStackTrace();
}
}
}
Upvotes: 4
Views: 122
Reputation: 9872
i don't know why you pass big array tempArr = new int[width*height]
to setpixel
method .i created a int col
array which represent white color and i passed it to setPixel()
method then i able to get complete white image as output.
int col[]={255,255,255};
WritableRaster raster1=img.getRaster();
for(int i=0;i<width;i++)
{
for(int j=0;j<height;j++)
{
raster1.setPixel(i, j, col);
}
}
Upvotes: 2