[n00b] how to add black trim/border/glow ?

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
jhoo
Posts: 7
Joined: 2018-08-26T03:16:06-07:00
Authentication code: 1152

[n00b] how to add black trim/border/glow ?

Post by jhoo »

very new to Imagemagick, trying to process a big batch of PNGs. as part of a larger process i want to go from A to B:

Image

so the idea is to get some black trim on the image bits

any suggestions how i would best go about this?

using IM 6.8.9.9-7ubuntu5.12 on linux Mint 18.3. . i'm going to be doing this as part of a bash script. this would be the last step of a larger process for each of the PNGs.
anyone who knows HexKit will recognize these as tiles for fantasy map-making.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [n00b] how to add black trim/border/glow ?

Post by snibgo »

The basic command could be:

Code: Select all

convert in.png +write mpr:INP -alpha extract -morphology dilate disk:5 \( +clone -fill Black -colorize 100 \) +swap -compose CopyOpacity -composite mpr:INP -compose Over -composite out.png
This adds a black line at transparency/opaque boundaries. A mask can prevent this from being added to the outside of the hexagon.
The hard part is preventing it from being added to the inside of the hexagon, except where the other boundary touches it.
snibgo's IM pages: im.snibgo.com
jhoo
Posts: 7
Joined: 2018-08-26T03:16:06-07:00
Authentication code: 1152

Re: [n00b] how to add black trim/border/glow ?

Post by jhoo »

omg! those IM commands are pure voodoo, but it almost worked.
it occurs to me that more info might help us get this nailed down. here is a progression of steps that i use to produce the final:

Image

so A is the starting image. i add the pre-defined border graphic B. this produces a pre-C (no inner trim), though i have to erase by hand (in GIMP) any pixels of A that might stick out past the outer edge of B because B is gospel: i can't mess with its outer perimeter and don't want to mess with its inner perimeter.

then (still in GIMP) i add the trim with the idea that i don't want to make B any wider:
- select all non-transparent (via "Alpha to Selection")
- shrink the selection by 1 px
- add a 2px black border with feathering

the point being that shrinking the selection _then_ adding the border means you don't bloat out the image any, though you do eat some of your A pixels which i take as a reasonable sacrifice.

FTR, D is what i get when i run your IM command on the pre-trim C image, in other words A+B.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [n00b] how to add black trim/border/glow ?

Post by fmw42 »

Perhaps you should post your separate starting images A and B, so we can start with the same images you did.
jhoo
Posts: 7
Joined: 2018-08-26T03:16:06-07:00
Authentication code: 1152

Re: [n00b] how to add black trim/border/glow ?

Post by jhoo »

sure thing, is there a preferred way to do attachments? i'm afraid i'm not seeing it.

in case it's okay to pull from imgur here are the share links:
the basic graphic, A: https://imgur.com/4r1bRUk
the border, B: https://imgur.com/J2b3Xeg
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [n00b] how to add black trim/border/glow ?

Post by fmw42 »

Here is one way. There might be a more efficient way.

#1 dilate the alpha channel of the object image to add your irregular border inside
#2 composite the ring image over the previous
#3 clone the alpha channel of the composite
#4 extract the alpha channel of the ring image and fill its interior with white to make a mask image to use to limit to the interior of the ring
#5 copy the alpha channel from #3 and the mask and multiply them
#6 delete the alpha channel and mask images keeping only the produce
#7 put the product into the alpha channel of the composite from #1 and #2
#8 save as result.png

Code: Select all

convert \
\( object.png -channel a -morphology dilate octagon:5 +channel \) \
ring.png -write mpr:ring -compose over -composite \
\( +clone -alpha extract \) \
\( mpr:ring -alpha extract -fuzz 99% -fill white -draw "color 100,100 floodfill" -alpha off \) \
\( -clone 1,2 -compose multiply -composite \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite object_ring_result.png
Image
jhoo
Posts: 7
Joined: 2018-08-26T03:16:06-07:00
Authentication code: 1152

Re: [n00b] how to add black trim/border/glow ?

Post by jhoo »

wow again! we're very close. A is what i'm hoping for, B is the result of that IM command:

Image

the two differences that i can see are that (a) the border on B is "added" to the outer edge of the graphic --thereby bulking it out a bit -- whereas in A it's done within the edge of the graphic, and (b) the "trim" on A is feathered (not sure if that's the right term, less jaggy is what i mean) compared to that on B.

we're definitely on the right track though.
gotta say, you IM guys are voodoo masters: those commands are the most cryptic thing i've seen in yonks.

that said, tyvm for your assistance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [n00b] how to add black trim/border/glow ?

Post by fmw42 »

I used your hexagonal ring image to overlay and to apply the limits. So there is no reason for it (B) to be larger than your other image (A) unless your GIMP processing did something else. But I modified my commands, since your A image seems to have eroded before adding the interior borders.

Code: Select all

convert \
\( object.png -channel a -morphology erode octagon:5 +channel \
-background black -alpha background \
-channel a -morphology dilate octagon:5 +channel \) \
ring.png -write mpr:ring -compose over -composite \
\( +clone -alpha extract \) \
\( mpr:ring -alpha extract -fuzz 99% -fill white -draw "color 100,100 floodfill" -alpha off \) \
\( -clone 1,2 -compose multiply -composite \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite object_ring_result2.png
Image
jhoo
Posts: 7
Joined: 2018-08-26T03:16:06-07:00
Authentication code: 1152

Re: [n00b] how to add black trim/border/glow ?

Post by jhoo »

i think you've nailed it! my sample on the left (A) and the new result from your IM command on the right (B).

Image

many thanks for this! of course it'll take me ages to begin to understand this but in the meantime i have what i need. tyvm.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [n00b] how to add black trim/border/glow ?

Post by fmw42 »

Looks like my inner border is just a bit thicker than yours. You can change that by changing the two octagon:5 values to octagon:4 or whatever works best.
jhoo
Posts: 7
Joined: 2018-08-26T03:16:06-07:00
Authentication code: 1152

Re: [n00b] how to add black trim/border/glow ?

Post by jhoo »

thank you, i will. just out of curiosity -- by no means a major issue -- is there a way to smooth out that inner border on yours a bit? up close at least it looks ever so slightly jaggy.
again, many thanks for this. please consider homage paid to your impressive IM skills.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [n00b] how to add black trim/border/glow ?

Post by fmw42 »

I am not sure this is better, but try

Code: Select all

convert object.png \
\( +clone -alpha extract -morphology erode octagon:4 \
-morphology edgeout octagon:6 -blur 0x2 -level 50x100% -alpha copy \) \
\( +clone -alpha off -fill black -colorize 100 \) \
+swap -compose over -composite \
ring.png -write mpr:ring -compose over -composite \
\( +clone -alpha extract \) \
\( mpr:ring -alpha extract -fuzz 99% -fill white -draw "color 100,100 floodfill" -alpha off \) \
\( -clone 1,2 -compose multiply -composite \) \
-delete 1,2 \
-alpha off -compose copy_opacity -composite object_ring_result3.png
Image
jhoo
Posts: 7
Joined: 2018-08-26T03:16:06-07:00
Authentication code: 1152

Re: [n00b] how to add black trim/border/glow ?

Post by jhoo »

as it happens i think it is better. works for me anyway so thanks again. i tip my hat to the ImageMagick voodoo master.
Post Reply