QImage Rotation
-
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!
-
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!
-
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!
@rtavakko Don't use QImage::transformed, instead set transform to your QPainter
-
Hi
as @Konstantin-Tokarev suggests , just use painter
http://www.bogotobogo.com/Qt/Qt5_QPainter_Transformation.php -
@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?
-
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);
}
-
@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?
@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?
-
@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?
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.
-
try the QPainter::SmoothPixmapTransform render hint applied to the painter and report back..
-
try the QPainter::SmoothPixmapTransform render hint applied to the painter and report back..
@Kent-Dorfman Yes, that fixed it. Thank you very much for your help!
-
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?
-
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?
@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 saysWhen 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.
-
@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 saysWhen 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.
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.