Important: Please read the Qt Code of Conduct -

Rotate QImage but do not alter size dimensions etc

  • Hello Devs :)
    I am making an application and a part of it rotates an image and saves it back (replacing the original).
    This is my code:
    @QImage image(ui->listWidget->currentItem()->text());
    if(!QFile(ui->listWidget->currentItem()->text()).exists() || image.isNull())
    QTransform rot;
    image = image.transformed(rot);>listWidget->currentItem()->text(), "JPG", 100);@
    So, this will rotate the image 90 degrees!
    If I run this code 4 times it should result to the initial image, but it doesn't. I can see major changes to the file size and image quality, even if the quality option is 100 (100%).

    So, I was wondering if it is possible to rotate an image without altering the dimensions (i don't know if this is happening) and mainly the quality of the image.
    For example, is it possible to have a 10002000 image, rotate it right 90 degrees and have a 20001000 image with exactly the same pixels as the initial? I mean the down left corner pixel to go up left corner, the next to it to follow etc without leaving back any pixel (if this was done I assume that this should result to the same image as the initial if the code was run 4 times but it doesn't)...
    Is this what the 100% quality does? I assume that no... How can I manage to do this?

    thanks in advance for any answers!

  • AFAIK, even a quality option of 100 will not use lossless compression (it is defined within the JPEG standard but not widely supported). Also, since the original file is compressed, you won't reach the same (compressed) state again without the original (uncompressed) image.

  • loladiro, thanks for the answer. I understood that it is not possible to do it via the way of the 1st post. Is there another way that takes the pixels one by one and place them using the way I described above or this is not possible using compressed formats like JPEG?

  • There are functions for lossless rotation within libjepeg (and espacially the jpegtran command line utility). You might be able to use them.

  • Ok thanks!

  • Moderators

    You could also might consider using .png images, which are lossless.

  • I use both :) Thanks for informing me that PNG is lossless :)

  • Actually, you should use the high quality transformation. Now you are using the fast:

    So, calling:
    image = image.transformed(rot, Qt::SmoothTransformation);
    is slower, but makes lots of better result.

    So, even by saving into lossless image file didn't help, because the image data was 'broken' just after the first transformation.

  • Moderators

    vivelu: Thanks for your input, but please note this thread is over 4 months old. Please be aware that posting to ancient (by forum standards) posts is typically frowned upon.


  • mlong: Thanks for the warning.

    I ended up here by google search and saw this topic had false or insufficient response to the problem stated. I kind of answered to myself and others who might be interested about Qt transformations. The more information about Qt is precise and complete, the more frowning I'm willing to accept =)

Log in to reply