MagickImage create soft edge that goes into 100% transparent

Viewed 803

My goal is to create a soft border on any picture where the edges go into 100% transparent color (You should not be able to see the edge of the image)

Here's the original image:

enter image description here

I found this (http://www.imagemagick.org/Usage/thumbnails/#rounded):

enter image description here

convert thumbnail.gif -alpha set -virtual-pixel transparent \
          -channel A -blur 0x8  -level 50%,100% +channel  soft_edge.png

Which is what I then have made this from:

var mImage = new MagickImage(image);
mImage.Format = MagickFormat.Png;
mImage.Alpha(AlphaOption.Set);
mImage.VirtualPixelMethod = VirtualPixelMethod.Transparent;

var form = mImage.Clone();
form.Level(new Percentage(50), new Percentage(100),Channels.Alpha);
form.Blur(100, 50,Channels.Alpha);
mImage.Composite(form);
mImage.Write(image);

And the result from that is this:

enter image description here

However, you can still clearly see the edges, and so they are not transparent enough.

When I take the image into Paint.NET I see that the edges are indeed somewhat transparent, but the edges are likely only 50% transparent, not 100%.

enter image description here

I have tried to adjust the Level percentages, the blur, and so on and so forth, but I cannot seem to get the edges to blur out properly.

How can I make the edges 100% transparent, so that the edges of the image becomes invisible when the image is used on the web?

2 Answers

Try this:

convert input.png -bordercolor black -fill white \
   \( -clone 0 -colorize 100 -shave 10x10 -border 10x10 -blur 0x10 \) \
   -compose copyopacity -composite output.png

C#

using (MagickImage mImage = new MagickImage(image))
{
    mImage.Format = MagickFormat.Png;

    using (IMagickImage mask = mImage.Clone())
    {
        mask.Format = MagickFormat.Png;

        mask.BorderColor = MagickColors.Black;
        mask.Colorize(MagickColors.White, new Percentage(100));
        mask.Shave(50, 50);
        mask.Border(50, 50);
        mask.Blur(0, 10);
        mImage.Composite(mask, CompositeOperator.CopyAlpha);

        mImage.Write(image);
        image.Position = 0;
    }
}

Edit:

I will try to put the code in a more script-like fancy so that it's easy for me. I'm sorry if it's not elegant:

var image = new MagickImage("input.jpg");
var mask = image.Clone();
mask.BorderColor = MagickColors.Black;
mask.Colorize(MagickColors.White, new Percentage(100));
mask.Shave(10,10);
mask.Border(10,10);
mask.Blur(0,10);
image.Composite(mask, CompositeOperator.CopyAlpha);
image.Format = MagickFormat.Png;
image.Write("output.png");

The example is almost correct. It works when the Composite action is removed. The command line example translates to this:

using (var image = new MagickImage("thumbnail.gif"))
{
    // -alpha set
    image.Alpha(AlphaOption.Set);

    // -virtual-pixel transparent
    image.VirtualPixelMethod = VirtualPixelMethod.Transparent;

    // -channel A means that the next operations should only change the alpha channel

    // - blur 0x8
    image.Blur(0, 8, Channels.Alpha);

    // -level 50%,100%
    image.Level(new Percentage(50), new Percentage(100), Channels.Alpha);

    // +channel cancels only allow operations on the alpha channel.

    image.Write("soft_edge.png");
}
Related