Home » Code » QImageWriter and Writing JPEGs

QImageWriter and Writing JPEGs

posted by Andy Maloney

If you are using the QImageWriter class in Qt to write JPEG files, you may have noticed that it creates large files.

For example, saving out a capture of a window in my app resulted in a 332 kB file. If I run that through ImageOptim [mentioned previously], this is reduced to 266 kB. Huh? What’s happening here?

Digging into the code, I looked at what jpegtran [which is what ImageOptim is running] did to reduce file size. It turns out there are two options you can turn on in libjpeg to reduce file size when writing out JPEGs: optimize and progressive scan. These are both lossless operations, so by turning both of these options on we can reduce the size of the image files without sacrificing any quality.

If I turn on just the optimize option, the file size reduces to 287 kB. Turning both optimize and progressive on reduces the files size to… 266 kB – the same as ImageOptim.

I should also note that writing PNGs using QImageWriter is almost useless given the size of the files it produces [at 100% quality]. The same window capture I mention above results in a 6.6 MB PNG! This can be reduced to a much more manageable 242 kB if you run it through ImageOptim. The problem is, unlike JPEG, the time to optimize a PNG is not trivial, so even if there are switches to do something similar, the time it takes to optimize would result in long delays writing out the PNG file. I haven’t fully investigated this, so there may yet be a solution.

To handle the JPEG issue, I filed a Qt report and patch: Support additional JPEG write options: ‘optimize’ and ‘progressive’ [QTBUG-20075].

The patch modifies the following files:

  • src/gui/image/qimageiohandler.cpp
  • src/gui/image/qimageiohandler.h
  • src/gui/image/qimagewriter.cpp
  • src/gui/image/qimagewriter.h
  • src/gui/image/qjpeghandler.cpp

It adds two options to the QImageWriter class: ImageOption::Optimize and ImageOption::Progressive. It is used like this:

Magic! Smaller JPEG files for free. I hope this patch will eventually make its way into the release version of Qt.

I can’t see any reason why the optimize option within libjpeg defaults to false in the first place – any ideas?

(19 Jan 2015): I submitted a patch for this which has been merged into the Qt codebase. This fix will be part of Qt 5.5.

Leave a Reply

Your email address will not be published. Required fields are marked *