Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Calculating image ratio
Forum Updated to NodeBB v4.3 + New Features

Calculating image ratio

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SikarjanS Offline
    SikarjanS Offline
    Sikarjan
    wrote on last edited by
    #1

    Hi,

    I would like to calculate the aspect ratio of an image and display the result. I do not actually want to modify the image, I just want to know the ratio. All I tried resulted in a ratio of 1. Why is that so?

        QImage image(imageFileName);
        double ratio = image.width()/image.height();
        qDebug() << image.width() << image.hieght() << "ratio" << ratio;
    

    The output is: 3264 2592 ratio 1

    kshegunovK 1 Reply Last reply
    0
    • SikarjanS Sikarjan

      Hi,

      I would like to calculate the aspect ratio of an image and display the result. I do not actually want to modify the image, I just want to know the ratio. All I tried resulted in a ratio of 1. Why is that so?

          QImage image(imageFileName);
          double ratio = image.width()/image.height();
          qDebug() << image.width() << image.hieght() << "ratio" << ratio;
      

      The output is: 3264 2592 ratio 1

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      Dividing an integer to an integer gives you an integer. Cast one of the integers to a floating point type before hand (also I advise reading a bit on implicit casts in C++).

      double ratio = static_cast<double>(image.width()) / image.height();
      

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      2
      • SikarjanS Offline
        SikarjanS Offline
        Sikarjan
        wrote on last edited by
        #3

        Sorry for the dumb question I am new to Qt and coming from PHP.

        Playing around I saw that Qt or QImage does not know the correct orientation of an image. So width is not always the width. I guess this is only a problem on OS X because it stores the rotation info differently. But I did not see an option get the orientation. I read that you need an extra library for that. Was there anything build in Qt recently so that I can do that from Qt?

        kshegunovK 1 Reply Last reply
        0
        • SikarjanS Sikarjan

          Sorry for the dumb question I am new to Qt and coming from PHP.

          Playing around I saw that Qt or QImage does not know the correct orientation of an image. So width is not always the width. I guess this is only a problem on OS X because it stores the rotation info differently. But I did not see an option get the orientation. I read that you need an extra library for that. Was there anything build in Qt recently so that I can do that from Qt?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @Sikarjan said in Calculating image ratio:

          Sorry for the dumb question I am new to Qt and coming from PHP.

          I didn't mean the question is dumb, I meant that Qt's just a library, not a language itself. You need to know (more or less) C++ to be able to use it, that's why I referred you to the C++'s language reference.

          @Sikarjan said in Calculating image ratio:

          So width is not always the width. I guess this is only a problem on OS X because it stores the rotation info differently. But I did not see an option get the orientation. I read that you need an extra library for that. Was there anything build in Qt recently so that I can do that from Qt?

          Do you mean the image's metadata? QImage should be aware of it and correctly load it, so width(), should be width.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1
          • SikarjanS Offline
            SikarjanS Offline
            Sikarjan
            wrote on last edited by
            #5

            I played around with the image scale example, which loads multiple images and puts them in a grid with 100 x 100 thumbnails. Here some images were rotated by 90°. I read in an older post that QImage cannot read the EXIF data of the image and therefore is not aware of the orientation.

            kshegunovK 1 Reply Last reply
            0
            • SikarjanS Sikarjan

              I played around with the image scale example, which loads multiple images and puts them in a grid with 100 x 100 thumbnails. Here some images were rotated by 90°. I read in an older post that QImage cannot read the EXIF data of the image and therefore is not aware of the orientation.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @Sikarjan said in Calculating image ratio:

              I read in an older post that QImage cannot read the EXIF data of the image and therefore is not aware of the orientation.

              Maybe, I don't know. If that's true and you need to read the EXIF header, then I think you indeed need an external library.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved