How to convert JPEG to JPEG XL (*.jxl) and back, losslessly

I was recently reading about the JPEG XL format, which is one of the promising options for replacing a venerable JPEG standard.

One of the aspects underlined in several sources is that it’s possible to losslessly transcode JPEG to JPEG XL and reduce the size of a file. What I found interesting is the possibility of converting the file back to the source JPEG without losing even a bit of the original information.

How to actually do it?

The talk is cheap, but finding a tool that can perform such a conversion took me some time. Here are the steps, though:

  1. Download a reference implementation of JPEG XL for your OS. For me, it was the jxl-x64-windows-static package.
  2. Convert your file using cjxl.exe encoder from the package:
    cjxl.exe input.jpg output.jxl
  3. Convert your file back using djxl.exe decoder:
    djxl.exe output.jxl input-restored.jpg
  4. Note that input.jpg and input-restored.jpg are indeed bit-perfect.
The result of the experiment. The JPEG file decoded from the JPEG XL image is indeed identical to the first one.

I was also trying to achieve the same using the ImageMagick tool, which is a more practical tool to work with images, but I don’t think such lossless transcoding is supported at the moment.

Thoughts and comments

It’s not surprising that new formats can achieve much better compression ratios. But I found it interesting that there is so much redundancy in JPEG that even a lossless compression can gain us significant benefits.

To put that in context, you can compare the compression ratio with popular archive types like zip or 7zip (with their “ultra” compression mode):

When a JPEG file is losslessly compressed to the ZIP, 7zip, and JPEG XL formats, JPEG XL shines with a much lower file size

It’s uncertain if the JPEG XL format will gain traction and widespread support as it competes with quality alternatives like AVIF and WEBP. But this legacy-friendly feature could still make it useful in some use cases. For example, efficient archiving of JPEG attachments uploaded by users that we must retain unmodified for later retrieval.

Leave a Comment