2

I have an image file on my desktop. I try to load it in Qt by QImage or QPixmap, but it has been rotated 90 degrees in the widget.

Source file dimension is 2448 x 3264.

QImage imdisplay(picPath);
QPixmap pp = QPixmap::fromImage(imdisplay);
ui->picpreview->setPixmap(pp.scaled(ui->picpreview->width(),ui->picpreview->height(),Qt::KeepAspectRatio));

qDebug() << imdisplay.size();
qDebug() << pp.width() << "x" << pp.height();

This is the qDebug log:

QSize(3264, 2448)
3264 x 2448

Where is the problem? image info

hamed
  • 63
  • 1
  • 6
  • 1
    What you see on your desktop application is not necessarily how the image does actually look like. Your desktop application may automatically rotate it. – vahancho Feb 16 '18 at 14:49
  • that is an image taken by my mobile camrea, it is correct everywhere (photoshop, preview application, windows os, but only in Qt image it is incorrect – hamed Feb 16 '18 at 15:05
  • How do you load the image file in Qt? Please show your code. – vahancho Feb 16 '18 at 15:08
  • just edited question , added code – hamed Feb 16 '18 at 15:12
  • Well, I still should insist that your debug output shows the actual dimensions of your image. – vahancho Feb 16 '18 at 15:38
  • but it is correct if i import it to cv::Mat (OpenCV) instead QImage – hamed Feb 16 '18 at 15:49
  • Any reason for using a `QImage` instead of directly using the `QPixmap`? – Aditya Feb 16 '18 at 16:01
  • I agree with @vahancho. A number of applications tend to intelligently change the image orientation despite the actual image's properties. I think the definitive way to determine might be to insert the said image to documents of `MS Word` or `Powerpoint` – Aditya Feb 16 '18 at 16:09
  • @Aditya i already tried QPixmap and Qimage both , same result, Only OpenCV had correct result, by the way this happens only for large size image in JPEG format – hamed Feb 17 '18 at 04:52
  • Can you if the problem is with in **any** large JPEG? Like, if you download some image from Google or just a photograph from a camera? – Aditya Feb 19 '18 at 14:55

1 Answers1

0

See this answer: https://stackoverflow.com/a/64721635/3605259

Your image file can contain transformation metadata, which other applications use to rotate it correctly (even Windows 10 image properties will show dimensions of image file after transformation).

To automatically rotate image using Qt when you read the file, you have to use QImageReader instead of QImage:

QImageReader reader("path-to-the-image-file");
reader.setAutoTransform(true);
QImage image = reader.read();