Page 1 of 1
[Solved ]Rotating image.. won't stay centered
Posted: 2016-01-31T08:50:24-07:00
by Rye
So, I tried rotating an image today.
This is my source:
[spoiler]
[/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:
[[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
Re: Rotating image.. won't stay centered
Posted: 2016-01-31T09:12:53-07:00
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.
Re: Rotating image.. won't stay centered
Posted: 2016-01-31T09:25:24-07:00
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 ?
Re: Rotating image.. won't stay centered
Posted: 2016-01-31T09:49:27-07:00
by Bonzo
You have a -crop; snibgo suggested an -extent
Re: Rotating image.. won't stay centered
Posted: 2016-01-31T10:09:09-07:00
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
Re: Rotating image.. won't stay centered
Posted: 2016-01-31T10:15:54-07:00
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
Re: Rotating image.. won't stay centered
Posted: 2016-01-31T10:51:55-07:00
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.]
Re: Rotating image.. won't stay centered
Posted: 2016-01-31T12:03:32-07:00
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
Re: [Solved ]Rotating image.. won't stay centered
Posted: 2016-01-31T12:30:42-07:00
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.
Re: [Solved ]Rotating image.. won't stay centered
Posted: 2016-01-31T15:19:29-07:00
by Rye
Perfect ! Thanks.
Honestly this should be written into the imgmagck examples page.
(If it isn't already)