13

Can anyone help me resize an image in qt without making the image pixelated. Here's my code. the result is not as good as the original quality..thanks...

QImage img(name);
QPixmap pixmap;
pixmap = pixmap.fromImage(img.scaled(width,height,Qt::IgnoreAspectRatio,Qt::FastTransformation));
QFile file(folder+"/"+name);
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "jpeg",100);
file.close();
Nejat
  • 31,784
  • 12
  • 106
  • 138
James Seva
  • 139
  • 1
  • 2
  • 7
  • Do you shrink or extend the image? If you extend the image you will always have a "big" quality loss, since there is no more information for the new space than in the original image. Nevertheless every resize will be a loss in quality. There are quite a few algorithms for this all having different pros and cons. – Sebastian Lange Mar 26 '14 at 06:37
  • Did you try using Qt::SmoothTransformation instead of Qt::FastTransformation? – h2so5 Mar 26 '14 at 06:37

1 Answers1

25

You have to pass Qt::SmoothTransformation transformation mode to the scaled function like:

QImage img(name);
QPixmap pixmap;
pixmap = pixmap.fromImage(img.scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
QFile file(folder+"/"+name);
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "jpeg",100);
file.close();
Nejat
  • 31,784
  • 12
  • 106
  • 138