Object Selection Tools

My first Krita plugin! I added two tools which allow to easily select distinct objects in the image. It doesn’t always work perfectly, but is often suprisingly spot on. Also the result masks are not super precise for now, but this could be improved. I think automated alpha-matte masks is also possible to add. Feedback is appreciated.

Short video for illustration

The project on GitHub

How does it work? It uses a slim version of an image segmentation AI trained by Meta on a whole lot of images they licensed.

It can run on CPU (default) or GPU (but only really practical on windows). It’s very responsive on decent hardware. On older machines with large resolutions it might take a few seconds.

How to try it out? This is a plugin library (no python):

  1. Krita 5.2.0 Beta1 is required!
  2. Plugin downloads can be found on GitHub.
  3. Windows: Extract the plugin ZIP into the Krita folder. Run Krita.
  4. Linux: Easiest way is to download the .appimage, make it executable and run it.

Is this safe? As safe as any other (python or c++) plugin. So basically not very safe at all.
But it’s quite easy nowadays to try out things in a VM or sandbox. For example, using firejail on Linux:

  1. Install firejail from your package manager
  2. Download the AppImage
  3. firejail --private --appimage krita-5.2.0-beta1-segmentation_plugin-1.0.0-x86_64.appimage
  4. (This will prevent Krita/Plugin to access your system or home directory)

Alternative ways to install:

  1. Linux: Use the official Krita AppImage and extract it. See ReadMe for instructions.
  2. It’s open source, so you can build it yourself.
  3. … I’m open to ideas, repackaging entire Krita is not very sustainable, maybe I missed some easier way…?
13 Likes

I used it, it has great features, both new tools are fast, and the results are also good.

How about adding it to the krita ontology? I heard it’s an Apache 2.0 license.

The box tool can support more types of box selection methods like the “close and fill tool”.
image

Automatic segmentation of layer content: Detect the content in the layer, copy the layers and use transparency masks separately to make them independent

5 Likes

I had a WIP code which enables extracting objects by changing selection after making a base selection, but thing is that there haven’t been much support for it and I was forced to abandon it entirely because I couldn’t build Krita any more. Maybe at least a working prototype can open up minds about this. It’s the only thing I miss from other software besides a few filters.

2 Likes

This is insanely useful and should totally be in the Krita main branch, in this form or another. But still, in there for everyone. The results in my painting are pretty damn good. Not perfect but save me many minutes compared to doing all the selections 100% by hand.

1 Like

Thank you so much for sharing! I think this will save me some time coloring in Krita since that’s the only issue I’m having… I really love Krita, but it’s missing some tools that I miss having in Photoshop and SAI so this plugin is nice to have on hand. It’s greatly appreciated! :heartpulse:

1 Like

This incredibly useful tool is kind of hard to install for Linux users so I just finished a shell script that will take the latest stable build of Krita and combine it with Acly’s tool for easier deployment:

To use the script; copy it and ctrl-shift-v to past it into terminal.

To install Krita with the object/segmentation selection tools:

echo "Installing Krita to ~/.Applications/Krita (hidden in your home directory)"
INSTALL_DIR=~/.Applications/Krita
mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"

echo "Getting the latest download for Krita and Segmentation Tools"
BASE_KRITA_URL="https://binary-factory.kde.org/job/Krita_Stable_Appimage_Build/lastSuccessfulBuild/artifact/"
KRITA_PAGE=$(curl -s $BASE_KRITA_URL)
LATEST_KRITA_APPIMAGE=$(echo "$KRITA_PAGE" | grep -o 'krita-.*\.appimage"' | head -1 | sed 's/"$//')
LATEST_PLUGIN=$(curl -s https://api.github.com/repos/Acly/krita-ai-tools/releases/latest | grep browser_download_url | grep tar.gz | cut -d '"' -f 4)

echo "Checking to see if your version of Krita is up to date?"
if [ ! -f "$LATEST_KRITA_APPIMAGE" ]; then
    echo "Downloading new Krita"
    wget -O "$LATEST_KRITA_APPIMAGE" "${BASE_KRITA_URL}${LATEST_KRITA_APPIMAGE}"
    chmod +x "$LATEST_KRITA_APPIMAGE"
    "./$LATEST_KRITA_APPIMAGE" --appimage-extract
else
    echo "Latest Krita is already installed; moving on."
fi

echo "Updating the desktop file with correct paths"
EXTRACTED_DESKTOP_FILE="$INSTALL_DIR/squashfs-root/org.kde.krita.desktop"
ICON_PATH="$INSTALL_DIR/squashfs-root/krita.png"
if [ -f "$EXTRACTED_DESKTOP_FILE" ]; then
    echo "Pointing shortcut to Krita and tools"
    sed -i 's|^Exec=.*|Exec=env APPDIR='$INSTALL_DIR'/squashfs-root APPIMAGE=1 '$INSTALL_DIR'/squashfs-root/AppRun %U|' "$EXTRACTED_DESKTOP_FILE"
    sed -i 's|^Icon=.*|Icon='$ICON_PATH'|' "$EXTRACTED_DESKTOP_FILE"
    cp "$EXTRACTED_DESKTOP_FILE" ~/.local/share/applications/
else
    echo "Extracted desktop file not found, it could have been moved."
fi

echo "Checking if segmentation plugin is outdated"
if [ ! -f "$(basename "$LATEST_PLUGIN")" ]; then
    echo "Downloading new plugin"
    wget -O "$(basename "$LATEST_PLUGIN")" "$LATEST_PLUGIN"
    tar -xf "$(basename "$LATEST_PLUGIN")" -C squashfs-root/
else
    echo "Latest segmentation plugin is already installed."
fi

echo "DONE! Installed Krita with Segmentation tools built in."
echo "For more info go here: https://github.com/Acly/krita-ai-tools"

The script will check whether you have the latest version; as it will keep the download files to check against to help you avoid unnecessary downloads. If you’re outdated, it will grab the latest Krita AppImage and plugin Tar; extracting the app image and then the tar into the extracted folder. Next the script grabs a copy of the included desktop file and modifies it to execute krita at the same time as the third party technology.

Meaning after you run this script; Krita and segmentation tools are installed and you can find it in the application list and pin the new Krita icon to your panel for easy access.

I also included a script to properly uninstall all associated data, and you even have the option of keeping your resource folder:

To uninstall:

# Ask for confirmation to uninstall Krita
read -p "Really, uninstall Krita? [Y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    # Define directories and files
    INSTALL_DIR=~/.Applications/Krita
    DESKTOP_FILE=~/.local/share/applications/org.kde.krita.desktop
    RESOURCE_DIR=~/.local/share/krita
    LOG_FILES=(~/.local/share/krita.log ~/.local/share/krita-sysinfo.log)

    # Ask whether to remove all related resources and log files
    echo "Uninstall all related resources and log files? [Y/N]"
    read -p "" -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        # Full Uninstall: Remove Krita, resource folders, and log files
        rm -rf "$INSTALL_DIR" "$DESKTOP_FILE" "$RESOURCE_DIR" "${LOG_FILES[@]}"
        echo "Full uninstall completed, including resources and log files."
    else
        # Standard Uninstall: Remove Krita and desktop file, preserve resources and logs
        rm -rf "$INSTALL_DIR" "$DESKTOP_FILE"
        echo "Uninstall completed, resources and log files preserved."
    fi
else
    echo "Uninstall aborted."
fi

It took me 4-6 long days to figure out how to make it; hope someone finds it useful!

However I would not have completed it without the help of @Michelist for pointing me to the right command for getting latest Krita stable from repo.

I hope this saves you time on assembling your favourite art tools.

Happy Krita painting.

6 Likes

This plugin is superb :ok_hand: sadly not working with 5.2.3. Trying to import krita_segmentation_plugin-windows-x64-1.0.2.zip gives this error. :cry:

image

You did not follow the plugin’s installation instructions. Depending on your OS they differ, go to GitHub and read them up if it is not described in this topic.

Michelist

2 Likes

You are entirely correct, user error strikes again :rofl:

1 Like

Does anyone know how to apply it to a Flatpak installation?

Hello @juuey12 and welcome to the forum :slight_smile:

It’s strongly advised that you use the official appimage for any attempt to get something working.

1 Like

Thanks! Glad to be here!

Yes I know that, but I prefer the Flatpak version for different reasons, e.g. with the .appimage I cannot make Tools → Scripts work

I’ve no idea why that should be but someone else may know about that subject.

I am having an issue with the plugin. When using the new bounding box tool provided by this plugin, for example after drawing a bounding box for object selection, the Krita will crash when trying to select any other tool, like brushes or what have you. Has anyone else experienced this issue?

:slight_smile: Hello @godlock2006, and welcome to the forum!

Important questions: Which OS are you using?
Do you use Krita 5.2.0, or, if not, which version of Krita are you using?

Michelist

Am I correct in assuming this does not work on Mac? I tried to install the plugin but it did not work.

This is correct and can be found out reading the extensive description on GitHub. The most user-friendly plugin is the Windows version, for Linux you have to use @Acly’s AppImage or unpack the original Krita AppImage and follow the instructions on GitHub, a macOS version does not exist.
Another user currently tries to compile a version of Krita with this plugin implemented for macOS.
If the user was successful I can’t tell, after asking for information he wasn’t seen again:

Michelist

1 Like

Hey sorry for the late reply. I am using 5.2.6.

Which only is half of the answer, but anyway, you are using a version of Krita that is not tested with the plugin, the only version known working is the 5.2.0, this may be the issue. So you can try it with the portable version 5.2.0 for Windows or the respective AppImage for Linux, both can be found here:

For any further help, you have to hope that @Acly will come back to this topic or, what will probably be the more effective way to reach @Acly, you report your issue on the GitHub Issue reporting site of this project. But even there you have to specify which operating system you are using in order to receive support.

Michelist

The latest version of the plugin has been working for all Krita 5.2.x releases so far, including 5.2.6. I will try to edit the information in the OP since it’s quite outdated and also links to an older release (but it doesn’t look like I can?).

I vaguely remember that a crash like that was a problem at some point, so if you are not using the latest release (currently 1.0.2) make sure to upgrade.

If you still encounter the crash create an issue on GitHub and include as much info as you can (OS/Krita/Plugin versions, what you were doing exactly, crash dumps). Can’t promise much unless I can reproduce it.

1 Like