Creating thumbnail images in Qt
Recently I need to create thumbnail images in one of my Qt applications, so I googled the Internet and found an article called Creating thumbnail preview by Ariya Hidayat. Ariya introduced three methods to create thumbnail images, all of them uses the QImage::scaled() function. Here’s a quick review of the three methods he introduced respectively.
The first method is using the nearest-neighborhood algorithm as following:
QImage image("/path/to/image");
QImage thumbnail = image.scaled(200, 200, Qt::KeepAspectRatio, Qt::FastTransformation);
The second is using the bilinear filtering to get a better quality, but the drawback is that it’s slower compared to the previous method:
QImage image("/path/to/image");
QImage thumbnail = image.scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation);
The third is tricky, Ariya mixed the previous two methods as he claimed this method has the similar quality to the second method but is even faster than the first method, and he called this method as “cheat scaling”, as following:
QImage image("/path/to/image");
QImage thumbnail = image.scaled(800, 800, Qt::KeepAspectRatio, Qt::FastTransformation).scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation);
However, after I tried all of these methods, I found that the performance is actually unacceptable even though I used the third method. My original images are usually at size around 4MB, and it spends too much time to create thumbnail images using any of above methods. Therefore, I tried to find other ways to improve the performance, and I did it. The answer is QImageReader class. Here’s the code I implemented to do the same thing as above:
int length = 200;
QImageReader image_reader("/path/to/image");
int image_width = image_reader.size().width();
int image_height = image_reader.size().height();
if (image_width > image_height) {
image_height = static_cast<double>(length) / image_width * image_height;
image_width = length;
} else if (image_width < image_height) {
image_width = static_cast<double>(length) / image_height * image_width;
image_height = length;
} else {
image_width = length;
image_height = length;
}
image_reader.setScaledSize(QSize(image_width, image_height));
QImage thumbnail = image_reader.read();
The code is much longer than using the QImage::scaled() function, but its performance is much better. Here’s the benchmark I tried to create thumbnail images for 10 jpeg images, and each of them has the size around 4MB.
![]()
As you can see, all three methods using QImage::scaled() are actually no much difference, but by using the QImageReader class, we got 46% performance improvement than using the Qt::FastTransformation. The algorithm QImageReader::setScaledSize() used for scaling depends on the image format. However, in this experiment, the quality of created thumbnail images are no difference between Qt::SmoothTransformation and cheat scaling and QImageReader methods.
For convenience, I implemented this function in my open source Qitty library. You can find this project at http://github.com/ollix/qitty. To create thumbnail images using this library, you’ll need to include the header file in your source code:
#include <qitty/image.h>
And use the qitty_utils::SacledImage() function:
QImage thumbnail = qitty_utils:ScaledImage("/path/to/image", 200);
Filed under: C++, Programming, Qt | 6 Comments
Tags: algorithm, C, cplusplus, function, image, library, processing, Qitty, Qt, thumbnail, toolkit




“Cheat scaling” only speeds up downscaling, not the image loading, hence it’s not an apple-to-apple comparison. Also, you should check the scaled quality. In addition, the second step in the cheat scaling method should be using my superfast halfscaling trick (linked in the blog entry).
How to make chart result test like that ? Are you use qtestlib ?
Nope, I just use Apple’s Keynote to generate the chart.
Sorry, I’m never use apple product
Well, You tried for 10 images. That’s value is average ?
Yes.