Summery of goal... You have an image that encloses a shape but with transparency both inside and outside. You want to overlay the border on the image but make the parts outside the border transparent RIGHT!
This needs two images... the border (transparent both inside and out) and a mask to cut the unwanted parts! A technique known as DOUBLE masking!
The tricky part is creating that outside cut mask!
WARNING: as you never gave a original transparency image.
Next time give the starting ACTUAL image!
The generation of the mask is tricky. because you not only need it to represent all the areas that need to be removed, )or retained. it does not matter which). But you also need it to NOT just stop at the very edge of the border (either the inside edge or outside edge. BOTH are bad.
What you want is for the mask to cover a little way INTO the border and beyond the anti-aliasing edging pixels. Better still the mask should extent to the very middle of the border!
This type of operation is known as morphology, and we apply it using the blur in then next command and Fred Weinhaus has a script for morphology type operations.
http://www.fmwconcepts.com/imagemagick/ ... /index.php
Also see Wikipedia
http://en.wikipedia.org/wiki/Mathematical_morphology
This type of operations have NOT been built into IM yet (but it will be added at some point)
This sounds easy but it isn't. What can make it difficult, especially in the example border given is how the line separates into two lines close to each other (on the right). You need to make sure that the space between these two lines remain classed as 'outside'.
In this example we do not have those 'close lines' on the inside of the image so we can generate the mask using the inside edge. So for this case it is easier to find a mask basied on the inside edge of the image.
OKAY steps.
If you had given me the original image I would first convert that to a mask using
-alpha extract and then use morphology to convert that down to a just a center line (a morphological skeleton!)
As you have NOT given me the original to work with. so I'll fudge a mask of the inside of the image.
Code: Select all
convert skinexample.png -colorspace gray \
-fuzz 38% -fill black -floodfill +50+50 white \
-fill white +opaque black
skin_mask_inside.png
the -fuzz percentage in the above should be made as large as posible without the inside leaking to the outside.
Now this make need to be morphologically expanded by a couple (2) pixels (morphological 'Dilation'). This uses a trick that was discovered by Fred Weinhaus, that does a square 'averaging' blur.
Code: Select all
convert skin_mask_inside.png -blur 2x65535 \
-threshold 99% skin_mask_inside_larger.png
Ok we now have out 'cut' mask.
Take the image and use that mask to make the outside parts transparent, and THEN overlay the border.
If you provide me with a copy of the ORIGINAL border image, I'll write all this up in IM Examples with images to follow!
Please contribute it. a more complex border would be better so we can go into more difficult cases, producing a more general method.