2

I would like to convert a jpeg image into png and to do so I am using the code below:

  QImageReader reader;
  reader.setFileName(imagePath);

  QImage image = reader.read();

  QImageWriter writer;
  writer.setFileName(newImagePath);
  writer.write(image);

I thought the output image would be exactly the same as the input one but the difference image is not null and I cannot figure out why. The difference image looks like a noise image with values ranging from -5 to 6.

I tried to do the same thing with another librairy called VTK but I don't have the same problem, the image before and after compression are exactly the same.

Any suggestion is welcome !

Marie
  • 31
  • 1
  • 4
  • I have testing your code with a jpeg on google and I have no problem, however my eyes is not expert. Have you more details ? – thibsc May 11 '17 at 12:10
  • Jpeg format assumes some compression. What if you use `QImageReader::setQuality()` and set the value to be 100 (corresponds to the maximum quality) for your jpeg image? – vahancho May 11 '17 at 12:26
  • What are `imagePath` and `newImagePath`? You should check the value returned from `writer.write(image)`? – G.M. May 11 '17 at 12:31
  • The computation of "the difference image" is done inside QImage, or are you using another decoder to load the jpeg image? – leonbloy May 11 '17 at 12:46
  • Thanks @Thibaut, the difference is not visible by eye, I only realized the images were different after computing the difference image. (I have an image processing tool to do that) – Marie May 11 '17 at 12:48
  • Thanks @vahancho, I tried to set the quality to 100 for the reader but the images are still different – Marie May 11 '17 at 12:49
  • Thanks @G.M.imagePath = `"C:/Work/Projets/Test/image.jpg"` and `newImagePath = "C:/Work/Projets/Test/image.png"`. I just checked the result of `writer.write(image)` ant it is equal to 1 – Marie May 11 '17 at 12:51

1 Answers1

0

Different JPEG decoders can produce slightly different RGB values (more so if the JPEG contains a ICC profile); there a lot of numerical rounding and conversions involved (however, encoders are supposed to differ in no more than one bit per pixel from the reference implementation, but I would not bet on that; see eg this answer and this one).

I suggest you try to do the pixel-by-pixel comparison inside QImage.

Community
  • 1
  • 1
leonbloy
  • 73,180
  • 20
  • 142
  • 190