Arrange image to circle

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

Arrange image to circle

Post by Rye »

Hi,

question,

how could I arrange multiple of these (named 001.png 002.png ... etc)

Image

to something like this:

Image

imgur.com/370e41f0-b0d2-49be-95f0-4672f039b489

using only imagemagick ?

- Thanks in advance.
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: Arrange image to circle

Post by GeeMack »

Rye wrote: 2018-10-27T01:09:04-07:00how could I arrange multiple of these (named 001.png 002.png ... etc) [...] using only imagemagick ?
Assuming you're in the directory containing the input images and that they're in the order you want them to appear in the output, a command like this would accomplish that task...

Code: Select all

magick *.png -gravity north -background none -extent 225x225 \
   -virtual-pixel none -distort SRT "%[fx:t*360/n]" -flatten result.png
If you're using Windows, replace that end-of-line backslash "\" with a caret "^".
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Arrange image to circle

Post by Rye »

Ah nice !
Supposing I'd change from say 12 to maybe 36 or perhaps 48 dashes:
Can your code be simply adapted to that ?
Also, out of curiosity: What is a "virtual pixel" ?
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: Arrange image to circle

Post by GeeMack »

Rye wrote: 2018-10-27T07:47:32-07:00Supposing I'd change from say 12 to maybe 36 or perhaps 48 dashes: Can your code be simply adapted to that ?
That FX expression "%[fx:t*360/n]" tells IM to rotate each input image in the list, designated by the "t", multiplied by 360 degrees divided by "n", the number of images in the list. It will still evenly rotate the images even if adding more images to the list. Learn more about FX expressions at THIS link.

If you're using the same image multiple times, you can read it in once then duplicate it as many times as necessary.

Code: Select all

magick dash.png -duplicate 11 ...
Also, out of curiosity: What is a "virtual pixel" ?
When you rotate something there will be some areas outside the image that will come into view. The virtual-pixel setting instructs IM on how to handle those pixels. Learn more about it at THIS link.
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Arrange image to circle

Post by Rye »

Ok, that makes sense.

Strange though however,
upon executing the command I get:
magick: invalid list of numbers '-distort' 't*360/nl]' at CLI arg 10 @ error/operation.c/LISimpleOperatorImage/2257


Nevermind, my mistake, your command needs the "%" escaped inside a bat file.

Works now. Perfect!
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Arrange image to circle

Post by Rye »

One More question (sorry for the double post, but I'm not sure this will be seen else)

Kind of semi related:

If I for example had a horizontal picture and wanted to bend it to a circle, like this:

Image

How'd I go about that ? Transform it ?
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: Arrange image to circle

Post by GeeMack »

Rye wrote: 2018-10-29T01:17:07-07:00If I for example had a horizontal picture and wanted to bend it to a circle, like this:
This example command starts by creating a red-to-yellow gradient image 400 pixels wide by 20 pixels high. Then it adds a transparent border to the top and bottom. Then it uses a version of "-distort" to bend it into a circle...

Code: Select all

magick -size 400x20 gradient:red-yellow ^
   -bordercolor none -border 0x10 -virtual-pixel none -distort arc 360 circle.png
These distort operations are very versatile and can get pretty complex, so you have to be careful of your virtual-pixel settings, background color, border color, etc., to make sure you get the results you want.

Learn more about using the "-distort" operator to make arcs and circles at THIS link. Look a bit further down that page for instructions on "-distort polar" which can also be used to bend images into circles.
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Arrange image to circle

Post by Rye »

Hmm...
for %%x in (*.png) do magick %%x -distort arc 360 %%x_out.png
creates a "globe like" sphere,
How can I modify its inner part to be empty however ? Also, is there a way to force it to use a predefined canvas size ? (as far a possible)


Ok, this seems to be getting better:

Code: Select all

for %%x in (*.png) do magick %%x -virtual-pixel Background  -background transparent -distort Arc 360 %%x_out.png

Just out of curiosity: Could Imagemagick *in theory* also "un-distort* an image back from a circle to a horizontal line ?
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: Arrange image to circle

Post by GeeMack »

Rye wrote: 2018-10-29T05:50:20-07:00How can I modify its inner part to be empty however ? Also, is there a way to force it to use a predefined canvas size ? (as far a possible)
There are additional arguments to the "-distort arc" operator that let you specify the inside and/or outside dimensions of the result. You can find that on the page I linked.
Just out of curiosity: Could Imagemagick *in theory* also "un-distort* an image back from a circle to a horizontal line ?
Well, sort of. Again, the page I linked shows how to use "-distort polar" and "depolar". That might get you part way there with varying quality of results.
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Arrange image to circle

Post by Rye »

Sort of is good enough for me here ;)

I doubt I'll ever need to go the reverse anyways.

Thanks. Worked out like a charm!
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: Arrange image to circle

Post by snibgo »

Rye wrote:Just out of curiosity: Could Imagemagick *in theory* also "un-distort* an image back from a circle to a horizontal line ?
Yes. I often do this, though in the opposite order: from a circle to a line, then back to a circle. An example is at De-barrel distortion.
snibgo's IM pages: im.snibgo.com
Post Reply