Rotating QRect using QTransform and Cropping a QImage with it
-
Hi,
i am trying to achieve the following, having a QRect of some size, i rotate it at its center and crop a QImage like below:
QRect cropRect(0, 0, height, width); QTransform transRect; QPoint centre = cropRect.Centre(); transRect.translate(center.x(), center.y()); tansRect.rotate(60.0); tansRect.translate(-centre.x(), -center.y()); cropRect = transRect.mapRect(cropRect); QImage croppedImage = originalImage.copy(cropRect);
well i figured out using the hard way that
tansRect.rotate(60.0);
does not exactly work like required here. it does rotate rectangle along center but the resulting rectangle is a bounding box of the rectangle that is rotated.one workaround was using a polygon
QPolygon transformedPoly = transRect.mapToPolygon(cropRect);
but all my attempts to get a QRect out of this polygon has gone in vain.
a failed attempt
cropRectangle.setTopLeft(transformedPoly.at(3)); cropRectangle.setTopRight(transformedPoly.at(2)); cropRectangle.setBottomRight(transformedPoly.at(1)); cropRectangle.setBottomLeft(transformedPoly.at(0));
any pointers are appreciated, thank !
-
Hi,
i am trying to achieve the following, having a QRect of some size, i rotate it at its center and crop a QImage like below:
QRect cropRect(0, 0, height, width); QTransform transRect; QPoint centre = cropRect.Centre(); transRect.translate(center.x(), center.y()); tansRect.rotate(60.0); tansRect.translate(-centre.x(), -center.y()); cropRect = transRect.mapRect(cropRect); QImage croppedImage = originalImage.copy(cropRect);
well i figured out using the hard way that
tansRect.rotate(60.0);
does not exactly work like required here. it does rotate rectangle along center but the resulting rectangle is a bounding box of the rectangle that is rotated.one workaround was using a polygon
QPolygon transformedPoly = transRect.mapToPolygon(cropRect);
but all my attempts to get a QRect out of this polygon has gone in vain.
a failed attempt
cropRectangle.setTopLeft(transformedPoly.at(3)); cropRectangle.setTopRight(transformedPoly.at(2)); cropRectangle.setBottomRight(transformedPoly.at(1)); cropRectangle.setBottomLeft(transformedPoly.at(0));
any pointers are appreciated, thank !
What are you expecting to get out of this process? A QImage is a rectangular area aligned with the axes, but you seem to be expecting something else.
I think what you want to do is rotate the image 60 degrees, crop it using a rectangle, and then maybe rotate the result back.
BTW: It would help if you posted (copy and paste) your actual code. That first snippet will not compile; there is no QRect::Centre() or variable tansrect.
-
What are you expecting to get out of this process? A QImage is a rectangular area aligned with the axes, but you seem to be expecting something else.
I think what you want to do is rotate the image 60 degrees, crop it using a rectangle, and then maybe rotate the result back.
BTW: It would help if you posted (copy and paste) your actual code. That first snippet will not compile; there is no QRect::Centre() or variable tansrect.
A QImage is a rectangular area aligned with the axes, but you seem to be expecting something else.
yes, i realized that.
I think what you want to do is rotate the image 60 degrees, crop it using a rectangle, and then maybe rotate the result back.
well that could also be done i guess, let me check and get back to you.
It would help if you posted (copy and paste) your actual code. That first snippet will not compile; there is no QRect::Centre() or variable tansrect.
i corrected it, please check the updated snippet.
Thanks !
-
What are you expecting to get out of this process? A QImage is a rectangular area aligned with the axes, but you seem to be expecting something else.
I think what you want to do is rotate the image 60 degrees, crop it using a rectangle, and then maybe rotate the result back.
BTW: It would help if you posted (copy and paste) your actual code. That first snippet will not compile; there is no QRect::Centre() or variable tansrect.
-
@ChrisW67 the actual code is not of a sample program and has un-necessary application logic which i feel is not required here. do let me know if there is anything else i can provide here.
Thanks.
@starkm42 said in Rotating QRect using QTransform and Cropping a QImage with it:
if there is anything else i can provide here
Yes. You can explain what you are expecting to achieve. What should the final image look like in relation to the original?
-
@starkm42 said in Rotating QRect using QTransform and Cropping a QImage with it:
if there is anything else i can provide here
Yes. You can explain what you are expecting to achieve. What should the final image look like in relation to the original?
@ChrisW67 need to crop a image from a large image, QImage.copy() accepts a QRectangle object (equivalent to cropping a section), now to get the rectangle we need to rotate one around the center at X, Y from origin where (X< OriginalImage.Width, Y < OriginalImage.Height and X + CropRectangle.Width() < OriginalImage.Width , Y + CropRectangle.Height < OriginalImage.Height).
something like below
-
Here is an idea you can try: Instead of a QImage use QPixmap. You can use a QPainter to draw to a pixmap. Now draw the original image rotated in the opposite direction onto the pixmap. From this rotated pixmap you can now do your cropping with a non-rotated rectangle.
-
Here is an idea you can try: Instead of a QImage use QPixmap. You can use a QPainter to draw to a pixmap. Now draw the original image rotated in the opposite direction onto the pixmap. From this rotated pixmap you can now do your cropping with a non-rotated rectangle.
@SimonSchroeder so rotating the painter itself and later cropping, yes i think it was suggested as well. and yes that seems to work for now. will be marking this solved.
-