Adding a colored aura around an image

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
Broadway
Posts: 2
Joined: 2019-08-31T14:13:03-07:00
Authentication code: 1152

Adding a colored aura around an image

Post by Broadway » 2019-08-31T14:23:29-07:00

I am currently trying to add an aura around an image using magick. Here is the test image I am using as the inputImage


I have a somewhat working solution at the moment using this command:

Code: Select all

magick -limit thread 1 books.png books.png ( +clone -alpha extract -blur 0x50 -fill Yellow -level 0,50% -background Yellow -alpha Shape +write g.png ) -compose DstOver -composite -background "transparent" -compose over -layers flatten out.png

However, it only produces a gradient grayscale aura Image

I have two questions.

How would I go about specifying the color of the aura?
Would there be a way to change the gradient color of a part of the image dynamically based on the color of the border pixel? I.e the aura is red when coming out a red book and blue when coming out a blue book?

User avatar
fmw42
Posts: 25588
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding a colored aura around an image

Post by fmw42 » 2019-08-31T15:01:06-07:00

The problem with adding an aurora around an image with transparency is that you will have to modify the transparency so that it is not fully transparent and fades. So you need to invert your gradient (so that it fades to black at the edges) and combine the gradient with the old alpha channel.

Image

Code: Select all

convert books.png \
\( -clone 0 \( -size 388x361 radial-gradient: -evaluate multiply 3 \) -compose plus -composite \) \
-alpha off -compose over -compose copy_opacity -composite \
books_aurora.png
Image

Broadway
Posts: 2
Joined: 2019-08-31T14:13:03-07:00
Authentication code: 1152

Re: Adding a colored aura around an image

Post by Broadway » 2019-08-31T15:14:46-07:00

fmw42 wrote:
2019-08-31T15:01:06-07:00
The problem with adding an aurora around an image with transparency is that you will have to modify the transparency so that it is not fully transparent and fades. So you need to invert your gradient (so that it fades to black at the edges) and combine the gradient with the old alpha channel.

Image

Code: Select all

convert books.png \
\( -clone 0 \( -size 388x361 radial-gradient: -evaluate multiply 3 \) -compose plus -composite \) \
-alpha off -compose over -compose copy_opacity -composite \
books_aurora.png
Image
I appreciate the help! I am mostly just wanting to change the color of the aura. So basically change the white part of the aura in the books_aurora.png to green, for example. Is this possible?

User avatar
fmw42
Posts: 25588
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding a colored aura around an image

Post by fmw42 » 2019-08-31T17:30:22-07:00

You need to change the background color under the transparency to your green color. Also you reduce the -evaluate multiply or remove it, since I only used that to exaggerate the white background so it would stand out.

Code: Select all

convert books.png -background green1 -alpha background \
\( -clone 0 -size 388x361 radial-gradient: -compose plus -composite \) \
-alpha off -compose over -compose copy_opacity -composite \
books_aurora_green.png
Image

Code: Select all

convert books.png -background green1 -alpha background \
\( -clone 0 \( -size 388x361 radial-gradient: -evaluate multiply 1.5 \) -compose plus -composite \) \
-alpha off -compose over -compose copy_opacity -composite \
books_aurora_green2.png
Image

User avatar
GeeMack
Posts: 719
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Adding a colored aura around an image

Post by GeeMack » 2019-08-31T21:21:15-07:00

Broadway wrote:
2019-08-31T14:23:29-07:00
How would I go about specifying the color of the aura?
If you want the "aura" to follow the contour of the image, you can colorize the image to a solid color of your choice, dilate it, and blur it, then composite the original back over that "aura" with a command like this...

Code: Select all

magick books.png -write mpr:input -fill gold -channel RGB -colorize 100 +channel ^
   -morphology dilate:2 disk -blur 0x8 mpr:input -composite out.png
That reads the input image and holds a copy of it in a memory register "mpr:input". Then it colorizes the RGB channels to gold, leaving the transparency as is. Next it dilates that gold silhouette to make it fatter all around, then blurs the outer edge of it. That will be the "aura". After that just bring that stored copy "mpr:input" of the original back into the command and composite it over the "aura".

You can easily adjust the dilation and blur to vary the amount of the aura to suit your need.

That command is in Windows syntax. To use it on a *nix system you'll have to change that continued line caret "^" to a backslash "\".
Would there be a way to change the gradient color of a part of the image dynamically based on the color of the border pixel? I.e the aura is red when coming out a red book and blue when coming out a blue book?
That would be substantially more complicated, maybe not even practical depending on the input image.

User avatar
fmw42
Posts: 25588
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding a colored aura around an image

Post by fmw42 » 2019-08-31T23:34:25-07:00

Would there be a way to change the gradient color of a part of the image dynamically based on the color of the border pixel? I.e the aura is red when coming out a red book and blue when coming out a blue book?
You can do that sort of using distort depolar/polar combination (cartesian to polar and polar to cartesian). However, possibly not quite what you want.

Image

Code: Select all

convert books.png -virtual-pixel edge -distort DePolar 0 books_x.png
Image

Code: Select all

convert books_x.png -write mpr:x \
\( +clone -alpha extract -morphology edgein diamond:1 \) \
-alpha off -compose copy_opacity -composite -scale x1! -scale 388x361! -alpha off \
mpr:x -compose over -composite books_y.png
Image

Code: Select all

convert \( books_y.png -write mpr:y -distort Polar 0 \) \
\( books.png -alpha extract -morphology dilate disk:5 -blur 0x20 \) \
-alpha off -compose copy_opacity -composite \
books_z.png
Image


Or adding a bit of blur:

Code: Select all

convert books.png -virtual-pixel edge -distort DePolar 0 books_x.png
Image

Code: Select all

convert books_x.png -write mpr:x \
\( +clone -alpha extract -morphology edgein diamond:1 \) \
-alpha off -compose copy_opacity -composite -scale x1! -scale 388x361! -alpha off -blur 0x2 \
mpr:x -compose over -composite books_y2.png
Image

Code: Select all

convert \( books_y2.png -write mpr:y -distort Polar 0 \) \
\( books.png -alpha extract -morphology dilate disk:5 -blur 0x20 \) \
-alpha off -compose copy_opacity -composite \
books_z2.png
Image


Each of these two sequences of 3 commands can be organized as one command line each. I just separate them into 3 steps each to show the process. 1) Depolar, 2) repeat outline vertically, 3) polar, dilate and blur original alpha channel and put that back onto polar image, which is back into normal cartesian view.

User avatar
GeeMack
Posts: 719
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Adding a colored aura around an image

Post by GeeMack » 2019-09-02T11:37:41-07:00

fmw42 wrote:
2019-08-31T23:34:25-07:00
You can do that sort of using distort depolar/polar combination (cartesian to polar and polar to cartesian).
Working from your idea of spreading the colors outward from the image, I came up with a few ways to create an aura or halo effect using some other standard IM operations.

Method 1...

Code: Select all

magick books.png -background none ^
   ( -clone 0,0,0,0 -motion-blur 0x10+%[fx:t*90] -blur 0x10 ) ^
   -reverse -layers merge out1.png
This first method clones the input image four times, then does a motion blur with each clone blurring a different 90 degree direction. It finishes by merging the original input image over those four directionally blurred layers. This is very slow. If you make the motion blurs just to the left and right with only two clones like "-clone 0,0 -motion-blur 0x10+%[fx:t*180]", it will still be very slow. ;)


Method 2...

Code: Select all

magick books.png -background none ^
   ( -clone 0 -virtual-pixel none -distort SRT "1.1 0" -blur 0x10 ) ^
   -reverse -layers merge out2.png
This second method also clones the input image, but then scales it up by 10% and blurs that. It finishes by merging the original input over that enlarged and blurred clone. You'll get a denser result by making more clones like "-clone 0,0,...".


Method 3...

Code: Select all

magick books.png -background none ^
   ( -clone 0,0 -spread 20 -blur 0x10 ) -reverse -layers merge out3.png
The last method clones the input image, then does a "-spread" operation on it to feather out the edges. It blurs the result and finishes by merging the input image over the spread and blurred layer(s). Increase the density of the result by adding more clones with "-clone 0,0,0,...".

The first method will only work on IM v7 because of the inline FX calculation. The other two should work with IM v6 just by changing "magick" to "convert".

These commands are in Windows syntax. For a *nix system you'll need to replace those continued line carets "^" with backslashes "\", and escape the parentheses "(...)" with backslashes "\(...\)".

User avatar
fmw42
Posts: 25588
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding a colored aura around an image

Post by fmw42 » 2019-09-02T11:58:38-07:00

Neat! Great ideas.

I like approach 2 best. It is fast, looks good and is simple with only one clone.

Post Reply