8

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);
    }
  }
Bugs Buggy
  • 1,514
  • 19
  • 39

1 Answers1

1

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.

sebkur
  • 658
  • 2
  • 9
  • 18
  • If that was the case, wouldn't the output image be compressed again if I provide the output image as input image? Say for example, an image, **image1** is 100kB , gets compressed to 60kB when I run the program on it and is saved as **output1**, now that 60kB image should get compressed again if I run the program on it and save it as **output2**, which isn't the case and **output2** is also 60kB. – Bugs Buggy Feb 12 '18 at 08:03
  • No, you can't generally compress multiple times with positive effects (reducing size). If that was the case, you could compress many times in a row and come up with a file of say only one byte. The resulting file would not be able to encode the image with a similar quality. Compression relies on detecting patterns in data and encoding them efficiently. With many common algorithms, no space-saving patterns can be detected when run a second time on the already compressed data. – sebkur Feb 12 '18 at 10:01
  • 1
    See this question on a general discussion of compressing data multiple times: https://stackoverflow.com/questions/3198666/why-can-data-be-compressed-only-once – sebkur Feb 12 '18 at 10:03
  • But I thought JPEG was lossy compression, not lossless? – Bugs Buggy Feb 12 '18 at 16:21
  • 1
    Yes, JPEG is lossy. Still, running JPEG compression multiple times does not reduce quality in each iteration. – sebkur Feb 13 '18 at 10:35