Magic Wand tool Image Filter
Magic Wand tool
To create a mask corresponding to a specific color area, you can take advantage of the new Magic Wand tool supported by the Aspose.Imaging library since the 23.8 release. Here's how you can use it:
- Begin by loading an image or photo.
- Create a new mask by using the
select
method of the MagicWandTool class. - To specify the color region you want to mask, provide the coordinates of a point inside that region using the MagicWandSettings object.
- The method will automatically select an area around the specified point with similar colors and tones using color comparison.
- You can adjust the size of the masked region by setting the
setThreshold
parameter, which accepts values between 0 and 100. This parameter acts as a tolerance level for comparing pixel colors. - Additionally, you have the option to combine the existing mask with a new one, subtract one mask from another, or invert the selection.
- To achieve smoother edges for the resulting mask, you can apply the
Feathering
method, specifying the desiredsetSize
parameter in pixels. - Finally, apply the mask to the image and save the final image in a format supported by the Aspose.Imaging library.
This process allows you to precisely select and mask specific color areas within your images or photos using the Magic Wand tool in the Aspose.Imaging library.
import com.aspose.imaging.Image; | |
import com.aspose.imaging.RasterImage; | |
import com.aspose.imaging.magicwand.*; | |
import com.aspose.imaging.magicwand.imagemasks.*; | |
// get path of the input data | |
String templatesFolder = System.getenv("DATA_PATH"); | |
// get output path | |
String outputFolder = System.getenv("OUT_PATH"); | |
if (templatesFolder == null) | |
templatesFolder = "data"; | |
if (outputFolder == null) | |
outputFolder = "out"; | |
try (RasterImage image = (RasterImage)Image.load(templatesFolder + "/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) {{ setThreshold(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() {{ setSize(3); }}) | |
// Apply mask to the image | |
.apply(); | |
image.save(outputFolder + "/result.png"); | |
} |