Adding "outer glow" to a PNG with transparent background

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
Marcesh
Posts: 3
Joined: 2016-02-17T11:02:38-07:00
Authentication code: 1151

Adding "outer glow" to a PNG with transparent background

Post by Marcesh »

Hi guys,

i have a few thousand transparent png product images
which i would need to add a background and an outer glow to. is this possible with ImageMagick and how would the convert command be?

This is what I have
Image

This is what it should be
Image

even better if the background could be filled with a seamless texture from an jpg file..

thanks for your help in advance
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding "outer glow" to a PNG with transparent background

Post by snibgo »

Marcesh wrote:i have a few thousand transparent png product images...
So why didn't you supply one? Instead, you supply an opaque example that has already been flattened against a chequer background. This means we can't experiment to work out an exact solution.

But an outline solution would be: extract the alpha, and blur. This would give a "glow" mask, but might need tweaking. Composite the original over that. Then flatten against a gray background, or whatever colour or other image you want.

Please also state the version of IM you use, and platform.
snibgo's IM pages: im.snibgo.com
Marcesh
Posts: 3
Joined: 2016-02-17T11:02:38-07:00
Authentication code: 1151

Re: Adding "outer glow" to a PNG with transparent background

Post by Marcesh »

sorry my mistake

here's the png with transparency
Image

I'm using ImageMagick 6.8.6-9
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding "outer glow" to a PNG with transparent background

Post by snibgo »

This is very close to your desired result. Windows BAT syntax.

Code: Select all

convert ^
  2lllxlv.png ^
  ( +clone ^
    -alpha extract ^
    -blur 0x10 ^
    -level 0,50%% ^
    -background White ^
    -alpha Shape ^
    +write g.png ^
  ) ^
  -compose DstOver -composite ^
  -background gray(42%%) ^
  -compose Over -layers flatten ^
  a.png
That gives a 42% gray background. If instead you want an image as background then ...

Code: Select all

convert ^
  2lllxlv.png ^
  ( +clone ^
    -alpha extract ^
    -blur 0x10 ^
    -level 0,50%% ^
    -background White ^
    -alpha Shape ^
    +write g.png ^
  ) ^
  -compose DstOver -composite ^
  your_background.jpg ^
  -compose DstOver -composite ^
  a2.png
EDIT: My commands include "+write g.png". They are for debugging (so you can sse just the "glow"). They can be removed when you are happy with the commands.
snibgo's IM pages: im.snibgo.com
Marcesh
Posts: 3
Joined: 2016-02-17T11:02:38-07:00
Authentication code: 1151

Re: Adding "outer glow" to a PNG with transparent background

Post by Marcesh »

thank you so so much! i would never have figured out this complex command
paleshadow123
Posts: 1
Joined: 2016-03-04T22:09:32-07:00
Authentication code: 1151

Re: Adding "outer glow" to a PNG with transparent background

Post by paleshadow123 »

snibgo wrote:This is very close to your desired result. Windows BAT syntax.

Code: Select all

convert ^
  2lllxlv.png ^
  ( +clone ^
    -alpha extract ^
    -blur 0x10 ^
    -level 0,50%% ^
    -background White ^
    -alpha Shape ^
    +write g.png ^
  ) ^
  -compose DstOver -composite ^
  -background gray(42%%) ^
  -compose Over -layers flatten ^
  a.png
That gives a 42% gray background. If instead you want an image as background then, and it is not about being in that ... If you see below for the info because when they convert it, it might look different and some might try this code and it works successfully.

Code: Select all

convert ^
  2lllxlv.png ^
  ( +clone ^
    -alpha extract ^
    -blur 0x10 ^
    -level 0,50%% ^
    -background White ^
    -alpha Shape ^
    +write g.png ^
  ) ^
  -compose DstOver -composite ^
  your_background.jpg ^
  -compose DstOver -composite ^
  a2.png
EDIT: My commands include "+write g.png". They are for debugging (so you can sse just the "glow"). They can be removed when you are happy with the commands.
This is awesome work. Looked up your page particularly your canny edge detection. Really robust code there but get's the job done. Will you be expanding on it and polish it up even more, if that's even possible? Great stuff though.
Last edited by paleshadow123 on 2018-05-15T17:00:47-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding "outer glow" to a PNG with transparent background

Post by snibgo »

I often make small edits to existing pages. I have about 30 new pages in preparation.
snibgo's IM pages: im.snibgo.com
hlynn
Posts: 1
Joined: 2019-07-24T16:02:13-07:00
Authentication code: 1152

Re: Adding "outer glow" to a PNG with transparent background

Post by hlynn »

Should this work with newer versions of Image Magick. I'm using 7.0.8-23 and it just creates a dark border around the image. Has something changed?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding "outer glow" to a PNG with transparent background

Post by fmw42 »

It works for me on IM 7.0.8.56 Q16 Mac OSX in Unix form

Code: Select all

magick -limit thread 1 \
2lllxlv.png \\
2lllxlv.png \
\( +clone \
-alpha extract \
-blur 0x10 \
-level 0,50% \
-background White \
-alpha Shape \
+write g.png \) \
-compose DstOver -composite \
-background "gray(42%)" \
-compose over -layers flatten \
a.png
Except there is a bug in OpenMP that is being attended to for the next release. So my result has some noise scattered about. But if I add (as I have) -limit thread 1, it fixes the issue until the fix is provided.

Please always provide your platform? Your version is rather old and I would suggest you upgrade as bugs are still being fixed.
Post Reply