1

I have used this code to get Image into array of pixels.

  • convertTo2DWithoutUsingGetRGB method for reading image to pixel array
  • writeToFile method for pixel array to image

Now I would like to convert the array of pixels to Image. But when I convert it, I am losing image data.

  1. Initial Image size: 80Kb JPG
  2. Duplicate Image size: 71Kb JPG

I can clearly notice some difference between the both images, the Java produced image has some sort of white-noise.

I would like to reproduce the image without single pixel loss of data, how do I achieve in Java?

RaceBase
  • 18,428
  • 47
  • 141
  • 202
  • 1
    Perhaps you haven't searched well. In this site itself you can find solutions : [question1](http://stackoverflow.com/questions/7618904/how-to-make-jpeg-lossless-in-java) and [question2](http://stackoverflow.com/questions/706665/lossless-jpeg-rotate-90-180-270-degrees-in-java). – blackSmith Aug 01 '13 at 07:48

1 Answers1

1

The jpg file format uses a lossy compression algorithm which means that the files it generates will have slight differences from the original. You can change the quality setting to compress more or less but you can't save the with its original size without any modifications.

This is why jpg isn't recommended for image editing. Use a lossless format instead, like PNG.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • It's looking bit different. Because the original image is JPG, why can't the same image be reproduced? It's like we can't copy original images using Java. – RaceBase Aug 01 '13 at 05:53
  • It's not java, it's the jpg file format. No image editing software can do what you're asking. If you want an exact copy you can use Files.copy – Joni Aug 01 '13 at 06:02
  • could be true. But if I want to just rotate the image and reproduce the copy of original image? or some other image operations? – RaceBase Aug 01 '13 at 06:18
  • That can be done without loss but it requires special knowledge of how the file format works internally and shuffling data around, not by reading the image as pixels. I think the standard libjpeg C library can do these operations, you can use jna or jni to call its functions from java – Joni Aug 01 '13 at 06:31
  • but not from Java? I tried with PNG format files, It's rotating and working fine. Though, why not with JPG or other file formats without loss of data? – RaceBase Aug 01 '13 at 06:40
  • Of course you can implement the same algorithm in Java, but if there's a library that already has it why no use it? As to your other question, maybe reading an overview of how JPEG compresses images by throwing away data answers it http://en.wikipedia.org/wiki/JPEG – Joni Aug 01 '13 at 06:56