Magic Wand tool Image Filter
Magic Wand tool
By using the MagicWandTool class from the Aspose.Imaging library, you can select a color area on a loaded image by specifying an arbitrary point within that area using the MagicWandSettings parameter. The Magic Wand tool will generate a mask based on color and tone comparisons with the surrounding pixels. You can adjust the mask's coverage by setting the Threshold parameter. A higher threshold will result in a larger mask area, potentially including unwanted parts of the image. Conversely, reducing the threshold allows you to create a mask that targets a more specific color area.
Once you've created a mask, you can add additional areas to it using the `Union` method, subtract regions from the existing mask, invert it, and smooth the edges using the `GetFeathered` method. Finally, you can apply the mask to the image and save the resulting image to disk.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.MagicWand; | |
using Aspose.Imaging.MagicWand.ImageMasks; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (Aspose.Imaging.RasterImage image = (Aspose.Imaging.RasterImage)Image.Load(Path.Combine(dataDir, @"template.png"))) | |
{ | |
// Create a new mask using magic wand tool based on tone and color of pixel {845, 128} | |
MagicWandTool.Select(image, new MagicWandSettings(10, 10)) | |
// Union the existing mask with the specified one created by magic wand tool | |
.Union(new MagicWandSettings(30, 30)) | |
// Invert the existing mask | |
.Invert() | |
// Subtract the specified mask created by magic wand tool from the existing one | |
.Subtract(new MagicWandSettings(50, 50) { Threshold = 69 }) | |
// Subtract four specified rectangle masks from the existing mask one by one | |
.Subtract(new RectangleMask(0, 0, 50, 50)) | |
// Feather mask with specified settings | |
.GetFeathered(new FeatheringSettings() { Size = 3 }) | |
// Apply mask to the image | |
.Apply(); | |
image.Save(Path.Combine(dataDir, @"result.png")); | |
File.Delete(Path.Combine(dataDir, @"result.png")); | |
} |