Page 1 of 1

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

Posted: 2018-08-27T04:36:01-07:00
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.

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

Posted: 2018-08-27T06:09:43-07:00
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.

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

Posted: 2018-08-27T09:57:54-07:00
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.

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

Posted: 2018-08-27T10:05:41-07:00
by fmw42
Perhaps you should post your separate starting images A and B, so we can start with the same images you did.

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

Posted: 2018-08-27T10:26:52-07:00
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

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

Posted: 2018-08-27T12:00:42-07:00
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

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

Posted: 2018-08-27T12:42:21-07:00
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.

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

Posted: 2018-08-27T14:18:50-07:00
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

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

Posted: 2018-08-27T15:04:03-07:00
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.

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

Posted: 2018-08-27T16:44:10-07:00
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.

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

Posted: 2018-08-27T22:49:58-07:00
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.

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

Posted: 2018-08-28T09:40:33-07:00
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

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

Posted: 2018-08-28T10:03:04-07:00
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.