[Solved ]Rotating image.. won't stay centered

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
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

[Solved ]Rotating image.. won't stay centered

Post by Rye »

So, I tried rotating an image today.

This is my source:

[spoiler]
Image
[/spoiler]

and after executing the following command:

Code: Select all

mogrify file.png -rotate 15 -background transparent file.png
convert file.png -crop 480x800+0+0 file.png
a few times, I got this:

Image
[[EDIT: The animated gif image won't show correctly in the browser, I rar'd it up it shows correctly for me in irfanview: ]]
[[https://dl.dropboxusercontent.com/u/145 ... oating.rar]]

Not how I imagined it.
I probably need some approach to keep it centered. right ?
[Bonus points for whomever can figure out a way to make this script easier... like: execute once and output a user set numbers of roations aka 001. 002. 003. etc.)

Thanks in advance
Last edited by Rye on 2016-01-31T12:08:33-07:00, edited 1 time in total.
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotating image.. won't stay centered

Post by snibgo »

The ordinary "-rotate" will make the output large enough to hold the rotated input. So the output at 45 degrees will be bigger than at 0 degrees or 90 degrees. The problem isn't centering, but sizing.

One cure is to do an "-gravity Centre -extent" after each rotation, so it always enlarges to the same size.
snibgo's IM pages: im.snibgo.com
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Rotating image.. won't stay centered

Post by Rye »

Hmm...
ok, this looks good:

Code: Select all

for %%x in (*png) do mogrify -rotate 15 -background transparent %%x
for %%x in (*png) do convert %%x -gravity Center -crop 480x800-96-49 +repage  %%x
Too bad I have to do the math myself... wish imagemagick did this auto"magickally".

Can be tiresome if too many files are required


EDIT: NOPE, this still produces the strange moving of the picture (not centered).
What am I doing wrong ?
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Rotating image.. won't stay centered

Post by Bonzo »

You have a -crop; snibgo suggested an -extent
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Rotating image.. won't stay centered

Post by Rye »

Hmm...

using "-extent" instead of "-crop" yields me the same results.... the image spirals downwards...

for %%x in (*png) do mogrify -rotate 15 -background transparent %%x
for %%x in (*png) do convert %%x -gravity Center -extent 480x800+0+0 +repage %%x
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotating image.. won't stay centered

Post by snibgo »

The only maths needed is to find the maximum space the image will occupy when rotated. The maximum space is a square, with sides equal to the diagonal of the image. Make the extent that size or larger.

For example, a Windows command to rotate the built-in "rose":

Code: Select all

for /L %i in (0,10,90) do convert rose: -rotate %i -gravity Center -extent 150x150 r_fr_%i.png
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotating image.. won't stay centered

Post by snibgo »

"identify 001.gif" tells us the size of your image. It is 480x800 on a 941x1008 canvas. So extending this to 480x800 won't help.

The diagonal of 480x800 is 933. Call it 1000. So we can "-extent" to 1000x1000.

Code: Select all

for /L %i in (0,10,90) do convert 001.gif -rotate %i -gravity Center -extent 1000x1000 +repage r_fr_%i.png
It wobbles as it animates because the logo isn't centred in the image, "-trim" solves that problem.

Code: Select all

for /L %i in (0,10,90) do convert 001.gif -trim -rotate %i -gravity Center -extent 1000x1000 +repage r_fr_%i.png
[EDIT: I forgot to put "-trim" in the command.]
snibgo's IM pages: im.snibgo.com
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Rotating image.. won't stay centered

Post by Rye »

Ok, so this:

Code: Select all

move *.png source.png
for /L %%i in (0,10,90) do convert source.png -trim -rotate %%i -gravity Center -extent 1000x1000 +repage r_fr_%%i.png
for %%x in (*png) do convert %%x -gravity Center -extent 480x800+0+0 +repage  %%x
is pretty much perfect in execution !

The only question I now have left is:
How can I get this to create 12 frames "aka" 180° rotation ?

EDIT: I guess executing the script x2 and renaming the files did the trick.

Thanks :)
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [Solved ]Rotating image.. won't stay centered

Post by snibgo »

My example rotated from 0 to 90 degrees, in steps of 10 degrees. You can change that to 0 to 180 degrees in steps of 15, or whatever you want.
snibgo's IM pages: im.snibgo.com
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: [Solved ]Rotating image.. won't stay centered

Post by Rye »

Perfect ! Thanks.

Honestly this should be written into the imgmagck examples page.
(If it isn't already)
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
Post Reply