3

I am instantiating a QImage from an image file like below and subsequently rendering it on a QWidget.

QImage ( const QString & fileName, const char * format = 0 )

For most images, everything works fine. But for a few images, the QImage gets loaded with a 90-degree rotated image.

It seems this happens only with pictures taken earlier on my phone in portrait mode. Those taken in landscape are fine

S B
  • 8,134
  • 10
  • 54
  • 108
  • 1
    Maybe the images contain some meta data about the orientation and the image viewers you are using rotate the image according to that information. –  Feb 27 '13 at 22:10

4 Answers4

4

With C++ Qt-class QImageReader:

QImageReader imgReader( imagePath );
imgReader.setAutoTransform( true );
QImage img = imgReader.read();
Joachim
  • 891
  • 9
  • 12
3

You might need to use a library like libexif to determine the photo orientation and then rotate the QImage accordingly

cppguy
  • 3,611
  • 2
  • 21
  • 36
  • 1
    Do you know which `libexif` API tells photo orientation? Better still is there a library built into Qt that can do the job? – S B Feb 28 '13 at 05:07
  • @SaptarshiBiswas I looked briefly and it doesn't look like Qt handles exif. The property for rotation in exif is called orientation I believe. libexif is a pretty simple library to use and has a very open license. It's easy to integrate into existing projects. – cppguy Feb 28 '13 at 19:14
3

Since Qt 5.5 set Image.autoTransform : true because the default is false!

QT Image QML Type reference

self.name
  • 2,341
  • 2
  • 17
  • 18
2

https://discussions.apple.com/thread/2541504?start=0&tstart=0

It sounds like it is a pretty common issue, where there is some flag or tag added on the image that says how to rotate it, instead of actually reordering the pixels in the image. For the image you are trying to render, you could go and take the format you are trying to use, and see if there are any extra flags you could check and have Qt do the rotation.

Sounds like cppguy knows of a library that can let you check these flags.

EDIT Found a better description for it:

johninsj - Re: iPhone 4 Photo's & Video Rotating Sideways In Email

Nov 2, 2010 1:45 PM (in response to VibrantRedGT)

Apple sets the jpeg meta tag for orientation when you shoot a photo, so if you hold the iphone upside down, or sideways, etc, the image (which is shot upside down or sideways, since the camera is upside down/sideways) knows it needs to flip/rotate the image when you look at it.

Not all software honors the rotation settings. Gimp (which runs on everything, and is free) does.

You can rotate images and save them, or learn to shoot photos with the iPhone in the correct orientation for non-rotated images. That would be with the home button to the right as you look at the screen.

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • Thanks for the nice explanation. Is there a way to read exif in Qt instead of using external libraries as cppguy suggested? – S B Feb 28 '13 at 06:12
  • These two questions: http://stackoverflow.com/questions/15128656/read-exif-metadata-of-images-in-qt and http://stackoverflow.com/questions/4105534/what-exif-lib-can-i-use-from-a-qt-program-on-embedded-linux?rq=1 may help. It looks like there is something, that you can add on: http://doc.qt.digia.com/qtextended4.4/qexifimageheader.html – phyatt Feb 28 '13 at 16:20
  • 1
    Since QExifImageHeader is not a part of Qt, I was doubtful about its support and maintenance aspects - so I went with libexif as cppguy suggested. – S B Mar 01 '13 at 05:08