Magic Wand tool Image Filter

Contents
[ ]

Magic Wand tool

         You can select a specific color region on a loaded image using the Magic Wand tool, which has been available since Aspose.Imaging library version 23.8. The MagicWandTool Class supports several useful methods, including select, union, invert, and subtract for manipulating masks. To select a color area, you need to specify a point with X and Y coordinates inside that area. The Magic Wand tool then uses the color and tone of that pixel as a reference and compares it with the surrounding pixels. If the select method finds these pixels to be similar, it adds them to the resulting area. Consequently, the method returns a mask representing the selected color area. An additional parameter, `threshold`, in the MagicWandSettings object sets the tolerance level for pixel color comparison. A higher threshold value increases color tolerance, leading to a wider selection of the color area.

         Moreover, you have the flexibility to perform various operations on masks. You can invert the selected mask, combine it with another mask using the union method, or subtract one mask from another. To refine the mask's appearance, you can utilize the `Feathering` method to smooth the mask edges, with the option to specify the desired feathering size in pixels. Once you're satisfied with the mask, apply it to the image to achieve the desired image effect.

import os
from aspose.imaging import Image, RasterImage
from aspose.imaging.magicwand import *
from aspose.imaging.magicwand.imagemasks import *
import aspose.pycore as ascore
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
out_path = os.path.join(data_dir, "result.png")
with ascore.as_of(Image.load(os.path.join(data_dir, "template.png")), RasterImage) as image:
wand_set = MagicWandSettings(50, 50)
wand_set.threshold = 69
f_set = FeatheringSettings()
f_set.size = 3
# Create a new mask using magic wand tool based on tone and color of pixel {845, 128}
tool = MagicWandTool.select(image, MagicWandSettings(10, 10))
# Union the existing mask with the specified one created by magic wand tool
tool = tool.union(MagicWandSettings(30, 30))
# Invert the existing mask
tool = tool.invert()
# Subtract the specified mask created by magic wand tool from the existing one
tool = tool.subtract(wand_set)
# Subtract four specified rectangle masks from the existing mask one by one
tool = tool.subtract(RectangleMask(0, 0, 50, 50))
# Feather mask with specified settings
tool = tool.get_feathered(f_set)
# Apply mask to the image
tool.apply()
image.save(out_path)
if delete_output:
os.remove(out_path)
view raw magic-wand.py hosted with ❤ by GitHub