Rotate image 4 and append to original to create "complete set"

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

Rotate image 4 and append to original to create "complete set"

Post by Rye »

So, as they say "a picture says more than a thousand words":

I want to go from this:

Image

to this:

Image

However I can't think of a way to rotate and append (while exanding the canvas without further instructing just how much) how exactly this might work.

Any help is appreciated.

-Rye-
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Rotate image 4 and append to original to create "complete set"

Post by GeeMack »

Rye wrote: 2017-03-09T11:41:59-07:00However I can't think of a way to rotate and append (while exanding the canvas without further instructing just how much) how exactly this might work.
Using "-distort SRT" allows for modifying the viewport size, rotating images on a particular center point, and finishing by moving that center point to another particular location all in one operation. Find out more about "-distort SRT" at THIS link.

Applying that "-distort ..." operation, combined with "-set option:distort:viewport" to set the output "window" size, and including some FX expressions to calculate sizes and rotations, you might come up with a command like this which does the whole thing...

Code: Select all

convert quarter.png -duplicate 3 -background none -virtual-pixel none ^
   -set option:distort:viewport "%[fx:w*2]x%[fx:h*2]" ^
   -distort SRT "%[w],0 1 %[fx:t*90] %[w],%[h]" +repage -flatten wholecircle.png
That uses Windows CMD syntax, but translating it to *nix shell should only require changing the carets "^" to backslashes "\".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rotate image 4 and append to original to create "complete set"

Post by fmw42 »

Here is another way (Unix syntax)

Code: Select all

convert quarter.png -trim +repage -write mpr:img \
\( mpr:img -flip \) +swap -append \
\( -clone 0 -flop \) +append \
result.png
Please always provide your platform/OS, since syntax differs between Unix and Windows.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Rotate image 4 and append to original to create "complete set"

Post by GeeMack »

Rye wrote: 2017-03-09T11:41:59-07:00However I can't think of a way to rotate and append (while exanding the canvas without further instructing just how much) how exactly this might work.
Another straightforward approach would be to duplicate the image, rotate one of the pair as necessary, and append them horizontally to make a half circle. Then duplicate that, rotate one of that pair so appending them vertically creates the whole circle. It still uses the "-distort SRT" operation I mentioned in my previous comment. That allows for conditionally rotating, scaling, etc., depending on and individual image's position in the stack. This would create the result in your example...

Code: Select all

convert quarter.png ^
   +duplicate -distort SRT "%[fx:t?-90:0]" +append ^
   +duplicate -distort SRT "%[fx:t?0:180]" -append wholecircle.png
Again, that's Windows syntax. Translating it to *nix should be a simple matter of changing carets "^" to backslashes "\".

The cloning, flipping, flopping, and appending method described by fmw42 above may be as simple as it gets.
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Rotate image 4 and append to original to create "complete set"

Post by Rye »

Nice !

Tried this one, out of the various suggestions:

Code: Select all

convert quarter.png ^ +duplicate -distort SRT "%[fx:t?-90:0]" +append ^ +duplicate -distort SRT "%[fx:t?0:180]" -append whole.png
and it works like a charm :)

Only one thing is strange:
When I try and modify it like this (as I'm a windows user.. sorry for not mentioning):

Code: Select all

[b]for %%x in (*png) do [/b]convert %%x ^ +duplicate -distort SRT "%[fx:t?-90:0]" +append ^ +duplicate -distort SRT "%[fx:t?0:180]" -append 0-%%x
the result images are... blank... any ideas on why this is ?
The code should still for, even if looped, right ?

Thanks again for the quick replies - you guys are awesome :D
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rotate image 4 and append to original to create "complete set"

Post by fmw42 »

Code: Select all

Why the [b] and [/b]?

Code: Select all

You have ^ characters that are not at the end of lines. Why?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Rotate image 4 and append to original to create "complete set"

Post by GeeMack »

Rye wrote: 2017-03-09T13:59:14-07:00When I try and modify it like this (as I'm a windows user.. sorry for not mentioning):

Code: Select all

[b]for %%x in (*png) do [/b]convert %%x ^ +duplicate -distort SRT "%[fx:t?-90:0]" +append ^ +duplicate -distort SRT "%[fx:t?0:180]" -append 0-%%x
the result images are... blank... any ideas on why this is ?
The code should still for, even if looped, right ?
The first thing I notice is you're using double percent "%%" signs on your loop variable like "%%x", and for the FX expressions in the IM command you only have single percent "%" signs like "%[fx:t?-90:0]". If you're running that code in a BAT script they all have to be double percent marks "%%". If you're running it from a command line they should all be singles "%".

Also, the carets "^" in your code line are not needed as long as there aren't line breaks at those locations. They aren't doing anything and they could cause problems with other parts of the code.
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Rotate image 4 and append to original to create "complete set"

Post by Rye »

Ah! Right, that variables aswell.

Thanks :) that worked !
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: Rotate image 4 and append to original to create "complete set"

Post by snibgo »

The flip-flop method seems the most natural one to me, but here is another method that is probably faster. (Not that speed matters for such a small image.) Windows CMD syntax.

Code: Select all

convert quarter.png -virtual-pixel mirror -set option:distort:viewport "%[fx:w*2]x%[fx:h*2]+0+%[fx:h]" -filter point -distort SRT 0 x.png
No image duplication, no rotation or appending, just one operation that visits each of the output pixels once.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Rotate image 4 and append to original to create "complete set"

Post by GeeMack »

snibgo wrote: 2017-03-10T04:51:05-07:00No image duplication, no rotation or appending, just one operation that visits each of the output pixels once.
I hadn't considered setting the geometry within the viewport setting. Thanks for mentioning that. I tried "-distort affine ..." and came up with a pretty simple method, too. It's similar to your example in that it uses "-virtual-pixel mirror" to get the dupes and mirrors, and gets there by sliding the image to a new location in the viewport.

Code: Select all

convert quarter.png -set option:distort:viewport "%[fx:w*2]x%[fx:h*2]" ^
   -filter point -virtual-pixel mirror -distort affine "0,%[h] 0,0" circle.png
Post Reply