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. QImage Rotation
QtWS25 Last Chance

QImage Rotation

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 8.7k Views
  • 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.
  • R Offline
    R Offline
    rtavakko
    wrote on last edited by
    #1

    Hey guys,

    I'm trying to create a customized qdial by making my own paintevent to display and rotate a QImage:

    void Knob::paintEvent(QPaintEvent *)
    {
    QPainter painter(this);

    QImage knobPix("C:\\Users\\Roham\\Documents\\Buzz\\knob.png");
    
    QMatrix rot;
    
    qDebug() << ((double)(this->value())/(double)(this->maximum()))*270;
    
    rot.rotate((double)((double)(this->value())/(double)(this->maximum())*270));
    
    QImage out = knobPix.transformed(rot);
    
    qDebug() << QString::number(this->width()) << "," << QString::number(this->height());
    
    painter.drawImage(QPoint(0,0),out);
    

    }

    This code doesnt rotate my QImage properly, it distorts and moves it around (it doesnt rotate the image around its center). It seems to be working fine for angles that are multiples of 90 degrees. I've tried translating the image to its width/2, height/2 and back afterwards with the same results.

    Any tips or advice you guys would have will be much appreciated!!

    Thanks a bunch!

    jsulmJ K 2 Replies Last reply
    0
    • R rtavakko

      Hey guys,

      I'm trying to create a customized qdial by making my own paintevent to display and rotate a QImage:

      void Knob::paintEvent(QPaintEvent *)
      {
      QPainter painter(this);

      QImage knobPix("C:\\Users\\Roham\\Documents\\Buzz\\knob.png");
      
      QMatrix rot;
      
      qDebug() << ((double)(this->value())/(double)(this->maximum()))*270;
      
      rot.rotate((double)((double)(this->value())/(double)(this->maximum())*270));
      
      QImage out = knobPix.transformed(rot);
      
      qDebug() << QString::number(this->width()) << "," << QString::number(this->height());
      
      painter.drawImage(QPoint(0,0),out);
      

      }

      This code doesnt rotate my QImage properly, it distorts and moves it around (it doesnt rotate the image around its center). It seems to be working fine for angles that are multiples of 90 degrees. I've tried translating the image to its width/2, height/2 and back afterwards with the same results.

      Any tips or advice you guys would have will be much appreciated!!

      Thanks a bunch!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @rtavakko Wouldn't it be easier and probably faster to use a set of pictures each one rotated relative to previous one? Then simply paint one after another (similar to animated GIF).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R rtavakko

        Hey guys,

        I'm trying to create a customized qdial by making my own paintevent to display and rotate a QImage:

        void Knob::paintEvent(QPaintEvent *)
        {
        QPainter painter(this);

        QImage knobPix("C:\\Users\\Roham\\Documents\\Buzz\\knob.png");
        
        QMatrix rot;
        
        qDebug() << ((double)(this->value())/(double)(this->maximum()))*270;
        
        rot.rotate((double)((double)(this->value())/(double)(this->maximum())*270));
        
        QImage out = knobPix.transformed(rot);
        
        qDebug() << QString::number(this->width()) << "," << QString::number(this->height());
        
        painter.drawImage(QPoint(0,0),out);
        

        }

        This code doesnt rotate my QImage properly, it distorts and moves it around (it doesnt rotate the image around its center). It seems to be working fine for angles that are multiples of 90 degrees. I've tried translating the image to its width/2, height/2 and back afterwards with the same results.

        Any tips or advice you guys would have will be much appreciated!!

        Thanks a bunch!

        K Offline
        K Offline
        Konstantin Tokarev
        wrote on last edited by
        #3

        @rtavakko Don't use QImage::transformed, instead set transform to your QPainter

        1 Reply Last reply
        4
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          as @Konstantin-Tokarev suggests , just use painter
          http://www.bogotobogo.com/Qt/Qt5_QPainter_Transformation.php

          1 Reply Last reply
          2
          • R Offline
            R Offline
            rtavakko
            wrote on last edited by
            #5

            @mrjj @Konstantin-Tokarev @jsulm Thank you guys for your replies. I think it would be easier to transform the original image as opposed to applying a transfomration to the last rotated image.

            I applied the transformation directly to the painter and the image now rotates correctly. But the image is still being distorted. Is there something I'm missing here?

            Kent-DorfmanK 1 Reply Last reply
            0
            • R Offline
              R Offline
              rtavakko
              wrote on last edited by rtavakko
              #6

              Hi guys, anyone have a solution to this? I've updated my paintEvent as you guys suggested and the image still distorts when it rotates. Angles that are multiples of 90 seem to be ok (0,90,180,360).

              void Knob::paintEvent(QPaintEvent *)
              {
              QPainter painter(this);

              painter.translate((double)(knobPix.width()/2),(double)(knobPix.height()/2));
              painter.rotate((double)((double)(this->value())/(double)(this->maximum())*360));
              painter.translate((double)(-knobPix.width()/2),(double)(-knobPix.height()/2));
              painter.drawImage(knobPix.rect(),knobPix);
              

              }

              1 Reply Last reply
              0
              • R rtavakko

                @mrjj @Konstantin-Tokarev @jsulm Thank you guys for your replies. I think it would be easier to transform the original image as opposed to applying a transfomration to the last rotated image.

                I applied the transformation directly to the painter and the image now rotates correctly. But the image is still being distorted. Is there something I'm missing here?

                Kent-DorfmanK Offline
                Kent-DorfmanK Offline
                Kent-Dorfman
                wrote on last edited by
                #7

                @rtavakko said in QImage Rotation:

                @mrjj @Konstantin-Tokarev @jsulm Thank you guys for your replies. I think it would be easier to transform the original image as opposed to applying a transfomration to the last rotated image.

                I applied the transformation directly to the painter and the image now rotates correctly. But the image is still being distorted. Is there something I'm missing here?

                Define "distorted". What exactly is distorted, and how?

                R 1 Reply Last reply
                0
                • Kent-DorfmanK Kent-Dorfman

                  @rtavakko said in QImage Rotation:

                  @mrjj @Konstantin-Tokarev @jsulm Thank you guys for your replies. I think it would be easier to transform the original image as opposed to applying a transfomration to the last rotated image.

                  I applied the transformation directly to the painter and the image now rotates correctly. But the image is still being distorted. Is there something I'm missing here?

                  Define "distorted". What exactly is distorted, and how?

                  R Offline
                  R Offline
                  rtavakko
                  wrote on last edited by
                  #8

                  @Kent-Dorfman 0_1554239424118_Untitled.png

                  The image is pixelated when rotated. Its most obvious around the edges. In the picture all the knobs except the bottom right one (this one is rotated at 0 degrees of rotation) are distorted. You can see the saw-like edges of the other ones. Let me know if you can see it from the picture.

                  1 Reply Last reply
                  0
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #9

                    try the QPainter::SmoothPixmapTransform render hint applied to the painter and report back..

                    R 1 Reply Last reply
                    5
                    • Kent-DorfmanK Kent-Dorfman

                      try the QPainter::SmoothPixmapTransform render hint applied to the painter and report back..

                      R Offline
                      R Offline
                      rtavakko
                      wrote on last edited by
                      #10

                      @Kent-Dorfman Yes, that fixed it. Thank you very much for your help!

                      1 Reply Last reply
                      0
                      • mgrondinM Offline
                        mgrondinM Offline
                        mgrondin
                        wrote on last edited by
                        #11

                        0_1565278071727_bad_rotation.gif

                        In my case I need to save to a file the transformed QImage. Scale (achieved by QImage::scaled) and crop (achieved by QImage::copy) works fine to produce a new QImage but the rotation result is not what I want.

                        Reading in the documentation:
                        https://doc.qt.io/qt-5/qimage.html#image-transformations
                        the transformation matrix is adjusted to compensate for unwanted translation, i.e. transformed() returns the smallest image containing all transformed points of the original image.

                        This seems to explain the behavior of the rotation. Is there a way to avoid the "compensation" mentioned above?

                        mrjjM 1 Reply Last reply
                        0
                        • mgrondinM mgrondin

                          0_1565278071727_bad_rotation.gif

                          In my case I need to save to a file the transformed QImage. Scale (achieved by QImage::scaled) and crop (achieved by QImage::copy) works fine to produce a new QImage but the rotation result is not what I want.

                          Reading in the documentation:
                          https://doc.qt.io/qt-5/qimage.html#image-transformations
                          the transformation matrix is adjusted to compensate for unwanted translation, i.e. transformed() returns the smallest image containing all transformed points of the original image.

                          This seems to explain the behavior of the rotation. Is there a way to avoid the "compensation" mentioned above?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @mgrondin
                          Hi
                          Looking pretty nice. :=)
                          I was wondering if
                          QTransform QImage::trueMatrix(const QTransform &matrix, int width, int height)
                          could be used as the docs says

                          When transforming an image using the transformed() function, the
                          transformation matrix is internally adjusted to compensate for
                          unwanted translation, i.e. transformed() returns the smallest
                          image containing all transformed points of the original image.
                          This function returns the modified matrix, which maps points
                          correctly from the original image into the new image.

                          as it sounds like what you are asking. Sorry if not. just asking.

                          mgrondinM 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @mgrondin
                            Hi
                            Looking pretty nice. :=)
                            I was wondering if
                            QTransform QImage::trueMatrix(const QTransform &matrix, int width, int height)
                            could be used as the docs says

                            When transforming an image using the transformed() function, the
                            transformation matrix is internally adjusted to compensate for
                            unwanted translation, i.e. transformed() returns the smallest
                            image containing all transformed points of the original image.
                            This function returns the modified matrix, which maps points
                            correctly from the original image into the new image.

                            as it sounds like what you are asking. Sorry if not. just asking.

                            mgrondinM Offline
                            mgrondinM Offline
                            mgrondin
                            wrote on last edited by
                            #13

                            Thank you @mrjj for the quick answer.

                            The QImage::trueMatrix does indeed return the modified matrix used by QImage::transformed.
                            Can the trueMatrix be used perform the inverse compensation transformation? From what I understand, the QImage::transformed function will always try to compensate thus leading in an even more distorted image.

                            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