Rotate image around center, without cropping

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
av01d
Posts: 6
Joined: 2013-10-02T03:24:45-07:00
Authentication code: 6789

Rotate image around center, without cropping

Post by av01d »

I'd like to rotate an image around its center. The docs say you can use -distort for this:

Code: Select all

convert [inFile] -distort ScaleRotateTranslate 30 [outFile]
This indeed works, but it also "crops" the image (actually, it retains the original image size).
I would like to "grow" the canvas to fit the rotated image. Using +distort:

Code: Select all

convert [inFile] +distort ScaleRotateTranslate 30 [outFile]
But now it rotates around the top-left corner, not around the center...

Does anybody know how I can rotate an image around its center, and growing the canvas to fit the result?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rotate image around center, without cropping

Post by fmw42 »

This works fine for me under IM 6.8.7.0 Q16 Mac OSX

Image

convert monet2.jpg -virtual-pixel white +distort SRT 30 monet_srt30.jpg

Image


What version of IM are you using and what platform?


But why not just use -rotate?

convert monet2.jpg -background white -rotate 30 monet_rot30.jpg
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotate image around center, without cropping

Post by snibgo »

After a rotation with "+distort SRT" or "-rotate", a "+repage" may be useful to remove the virtual canvas data. This is automatically removed for output to jpeg, but not for png, and might be the reason for the OP's problem.

Code: Select all

convert [inFile] +distort ScaleRotateTranslate 30 +repage [outFile]
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rotate image around center, without cropping

Post by fmw42 »

After a rotation with "+distort SRT" or "-rotate", a "+repage" may be useful to remove the virtual canvas data
This is probably a good idea. However, as a test, I tried my command above and it did not display any virtual canvas issues using png output. So I am wondering if there might be a virtual canvas already in the input image, which would also need to be removed by the +repage.

We would have to see the input image to know for sure. Can that be provided? You can post it to some free image hosting service such as dropbox and then put a link here. Also please supply your IM version and platform.
Last edited by fmw42 on 2013-10-03T21:11:58-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotate image around center, without cropping

Post by snibgo »

I do get offsets on PNG output with IM v6.8.7-0 on Windows 7:

Code: Select all

D:\web\im>%IM%convert monet2.jpg -background pink -virtual-pixel Background +distort SRT 20 m.png

D:\web\im>%IM%identify m.png
m.png PNG 365x405 365x405-50-36 8-bit sRGB 175KB 0.000u 0:00.000

D:\web\im>%IM%convert monet2.jpg -background pink -rotate 20 m.png

D:\web\im>%IM%identify m.png
m.png PNG 365x405 365x405-50-36 8-bit sRGB 175KB 0.000u 0:00.000
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rotate image around center, without cropping

Post by fmw42 »

Yes, you are right. I should have checked further. In looking at the verbose info I see


Page geometry: 397x423-66-45
Origin geometry: -66-45


However, I had no problem in displaying the resulting image and did not see any shifts. Could just be the browsers and my Mac Preview do not respect the virtual canvas or ignore negative offsets.
av01d
Posts: 6
Joined: 2013-10-02T03:24:45-07:00
Authentication code: 6789

Re: Rotate image around center, without cropping

Post by av01d »

Turns out my problem had nothing to do with convert...
Users can rotate text on an image in their browser. I use CSS3 transforms for that (which rotates at the center).
Then I send the x,y coordinates of the text on the image, as well as the angle to the server.
On the server, the text is 'composed' over the image.
I simply forgot to recalculate the x,y position as a result of the new canvas size.
Now that this calculation is in place, the rotation works beautifully.
Thanks for all your replies!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Rotate image around center, without cropping

Post by anthony »

By default the +distort SRT transform will rotate images about center and even preserve that center point position as a 'layer' image.

You can select any point about which to rotate, and even move that point to any sub-pixel location in the destination layer image.

-rotate itself is now a 'shortcut' to the more useful SRT distortion.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
don.yogesh
Posts: 1
Joined: 2014-03-09T10:19:32-07:00
Authentication code: 6789

Re: Rotate image around center, without cropping

Post by don.yogesh »

Hey I am also facing the same issue. How you calculated the new x,y position? I also tried couple calculation but was not able to calculate proper x,y . It is still moving image location.
Can you please provide the logic to calculate new x,y position?

Thanks

av01d wrote:Turns out my problem had nothing to do with convert...
Users can rotate text on an image in their browser. I use CSS3 transforms for that (which rotates at the center).
Then I send the x,y coordinates of the text on the image, as well as the angle to the server.
On the server, the text is 'composed' over the image.
I simply forgot to recalculate the x,y position as a result of the new canvas size.
Now that this calculation is in place, the rotation works beautifully.
Thanks for all your replies!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rotate image around center, without cropping

Post by fmw42 »

What exactly is your problem and what is your IM version, platform and exact command line.

If your image has no virtual canvas, then -rotate or +distort SRT will not crop your image and you do not need to provide the center points. If you have a virtual canvas, then add +repage after reading in the input image to remove it.

It is also better etiquette to make a new post and not tack onto an old post that is likely solved and may not be your exact problem.

If you have rotated image and need the new center coordinates for some reason, then in Unix, you can compute a variable that has the new center coordinates

center=`convert rotatedimage -format "%[fx:w/2],%[fx:h/2]" info:`

I do not know the equivalent in Windows syntax
Post Reply