Qt中QImage/QPixmap加载图片异常旋转问题排查
Hey there! Let's fix that rotated image issue
First off, the most likely culprit here is the EXIF Orientation metadata embedded in your image. When you take a photo in portrait mode with a phone or camera, the sensor often captures the image in landscape-style pixel storage (like your 2448×3264, where width is smaller than height), but adds an EXIF tag telling viewers to rotate it 90 degrees for proper display.
The problem? Qt's default QImage and QPixmap loaders don't automatically respect this EXIF orientation tag. So they render the image exactly as the raw pixel data is stored, which makes it look rotated compared to what you see in your desktop image viewer.
How to fix it
You have two solid options depending on your Qt version:
Option 1: Manual EXIF orientation handling (Qt 5.5+)
Use QExifImageHeader to read the orientation tag and rotate/mirror the image accordingly:
#include <QExifImageHeader> // Load the original image from your desktop QImage image(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/your-image.jpg"); QExifImageHeader exifHeader(image); // Check if the orientation tag exists in EXIF data if (exifHeader.contains(QExifImageHeader::Orientation)) { int orientation = exifHeader.value(QExifImageHeader::Orientation).toInt(); // Apply the correct transformation based on the orientation code switch(orientation) { case 2: image = image.mirrored(true, false); break; // Flip horizontally case 3: image = image.rotated(180); break; // Rotate 180° case 4: image = image.mirrored(false, true); break; // Flip vertically case 5: image = image.mirrored(true, false).rotated(90); break; // Flip + rotate 90° case 6: image = image.rotated(90); break; // Your case is almost certainly this one case 7: image = image.mirrored(true, false).rotated(270); break; // Flip + rotate 270° case 8: image = image.rotated(270); break; // Rotate 270° default: break; // No transformation needed } } // Now use the corrected image for display (e.g., on a QLabel) yourDisplayLabel->setPixmap(QPixmap::fromImage(image));
Option 2: Auto-transform with QImageReader (Qt 6.5+)
If you're on Qt 6.5 or newer, this is the simpler route—just enable auto-transformation to let Qt handle the EXIF orientation automatically:
#include <QImageReader> #include <QStandardPaths> QImageReader reader(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/your-image.jpg"); reader.setAutoTransform(true); // Tells the reader to respect EXIF orientation tags QImage image = reader.read(); // The image is now in the correct orientation for display yourDisplayLabel->setPixmap(QPixmap::fromImage(image));
How to verify the issue
To confirm this is exactly what's happening, you can:
- Check your image's EXIF properties in a desktop viewer (like Windows Photos or macOS Preview) to see the "Orientation" value. For your case, it should be set to "Rotate 90° clockwise" (which maps to code 6 in the EXIF standard).
- Add a quick debug line to print the orientation value directly from Qt:
qDebug() << "EXIF Orientation Code:" << exifHeader.value(QExifImageHeader::Orientation).toInt();
内容的提问来源于stack exchange,提问作者hamed

