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);
}
}
Each cell of the array, corresponds to a pixel in the image. This means that the array's dimensions are equal to the resolution. Therefore, a color image with a resolution of 1920 x 1080 pixels will be broken down to a 3 arrays with the same dimensions.
Images are stored in the form of a matrix of numbers in a computer where these numbers are known as pixel values. These pixel values represent the intensity of each pixel. 0 represents black and 255 represents white.
image are arrays, where the image is described by the function F(x, y), the coordinates being integers where each point of the image has a certain location and value, called Picture Element (pixel).
Description. The pixels[] array contains the values for all the pixels in the display window. These values are of the color datatype. This array is defined by the size of the display window.
Pixelator is a smart software to convert images into pixel art sprites and cover arts. With Pixelator you can use any source picture to easily generate Pixelated graphics for games or posters.
You can try setting the compression level to different values and see how the file size differs depending on that. See Setting jpg compression level with ImageIO in Java for how to do that.
Doesn't explain the change in brightness and contrast though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With