This Python script allows for interactive image masking using either AI-based segmentation with Segment Anything 2.1 or manual painting/lasso selection in a UI.
- AI Mode (Segment Anything 2.1)
- Hovering over objects previews a mask using the tiny model.
- Clicking applies a mask using the large model.
- AI Mode (Florence 2 + Segment Anything 2.1)
- Provide a prompt along with input and output folders to automatically mask every image in the folder
- Manual Masking Mode
- Lasso Selection (Default Mode)
- Press
Lto toggle lasso mode. - Left click: Lasso a white mask.
- Middle click: Lasso a grey mask.
- Right click: Erase mask.
- Press
- Paintbrush Mode
- Left click: Paint a white mask.
- Middle click: Paint a grey mask.
- Right click: Erase mask.
- Lasso Selection (Default Mode)
- Zooming
- Use the mouse wheel to zoom in and out.
- Saving
- Press
Sto save mask
- Press
- Reset
- Press
Rto reset masks
- Press
- Fullscreen
- Press
escto fullscreen
- Press
- Navigation
- Press
leftandrightkeys to navigate between files.
- Press
- Toggle AI Mode
- Press
tabto toggle AI mode (off by default)
- Press
Ensure you have Git and Python installed, then run the following commands:
git clone https://github.com/Tophness/Segment-Anything-Mask-Generator
cd Segment-Anything-Mask-Generator
python -m venv venv
call venv\Scripts\activate
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu126
pip install -r requirements.txtRun the script with:
python main.pyYou can also provide input and output folders to automatically launch in manual / SAM 2.1 mask editing mode using those folders, choose a file in the folders to start from and use cpu if you don't want to use gpu:
python main.py --input_folder "folderpath" --output_folder "folderpath" --start_file "start_file.png" --cpuSelecting a hand in AI mode
Drawing a white mask in lasso mode
Removing part of a mask in lasso mode
Drawing a grey mask in paintbrush mode
This project is released under the MIT License.



