4
votes

I have a question with regard to JPEG file writing. Suppose I have a PNG file example.png, and I want to change the file format to JPEG without any information loss. For the now being, I have two solutions:

  • Solution 1: perform the file formatting transformation with MATLAB

    I = imread('example.png');

    imwrite(I,'example.jpg','Mode','lossless');

    II = imread('example.jpg');

    differ = I-II;

    max(differ(:))

    This solution can produce lossless JPEG files. However, the problem with this solution is that some information in the original image such as the DPI resolution may be lost. Moreover, the produced output image cannot be viewed by popular image viewers such as IrfanView and Windows Paint.

  • Solution 2: use IrfanView software. Use the "Save as" function of IrfanView program, we can change the file format very easily. However, although I have set 'best quality 100' option when saving the JPEG file, the output image also show some information loss. The difference between these two images are not zero for all the pixels.

I am therefore wondering what I should do in order to solve the problem. Any ideas will be appreciated.

1
It is very easy to solve your problem: stay with the PNG image.mmgp
Jpeg2000 is a lossless jpeg file format, but unfortunately virtually no software supports jpeg2000. .png and .gif are the best choice for lossless files.Fred F
Your first assumption needs to change. "normal" JPEG images use a lossy compression format. As @mmgp says, staying with PNG is a good option. If you are looking for a better compression ratio, then you will need to give up the idea of lossless.BitBank

1 Answers

2
votes

This problem has no solution (yet, as of 2018).

You can't avoid use of lossy compression if you want the JPEG file to be usable in majority of image viewers.

The commonly supported version of JPEG is based on DCT compression, which - by definition - performs transformation and rounding that causes some precision loss.

The alternative, lossless JPEG compression method, JPEG-LS, is rarely supported.

There's also JPEG-XT extension that's a combination of lossy image + layer for reconstruction of the lossless original. It fails gracefully in JPEG image viewers, but it's even newer, and I don't know if it's implemented anywhere yet.

If you really need lossless, use PNG. With JPEG the best you can get is minimally lossy JPEG in RGB colorspace at quality=100 (which isn't literally 100%).