I converted an image into an array of pixels. I saved RGB values in three seperate arrays.
Then, I tried creating the image using the same values (no manipulation). Original image was of 205kB and the new image is of 121kB in case of B&W image, and from 215kB to 96kB in case of colored image. Also, there is slight change in brightness (brightness gets increased and so does overall contrast). What is causing this?
I have tried both with colored and B&W images. Result was same.
Also, I ran the same code on the previous output image (96kB), the new output was still 96kB.
Codes-
1) To read image:
int width = img.getWidth(null);
int height = img.getHeight(null);
pixelR = new int[width * height];
pixelG = new int[width * height];
pixelB = new int[width * height];
int index=0;
int r, g, b, gray, rgb;
for(int i=0; i<width; i++) {
for (int j=0; j<height; j++) {
rgb = img.getRGB(i, j);
r = (rgb >> 16) & 0xFF;
g = (rgb >> 8) & 0xFF;
b = (rgb & 0xFF);
pixelR[index]=r;
pixelG[index]=g;
pixelB[index]=b;
index++;
}
}
2) Write image:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int index=0;
int val;
int r, g, b;
for (int i=0; i<width; i++) {
for (int j=0; j<height; j++) {
r=pixelR[index];
g=pixelG[index];
b=pixelB[index++];
Color newColor = new Color((int)r, (int)g, (int)b);
image.setRGB(i, j, newColor.getRGB());
}
}
File f = null;
try{
f = new File("<filepath>.jpg");
ImageIO.write(image, "jpg", f);
}catch(IOException e){
System.out.println("Error: "+e);
}
}