Search code examples
gimpscript-fu

how would I turn a "magic-wand" selection into a path object in GIMP using batch scripting


As the title suggest, how would one perform this? Right now I have this plugin to add a basic path to an image, but how do I convert a selection from "gimp-image-select-contiguous-color" into "vectors"?

#!/usr/bin/env python

from gimpfu import pdb, main, register, PF_STRING

def add_path(infile, outfile):
    image = pdb.gimp_file_load(infile, 'image')
    vectors = pdb.gimp_vectors_new(image, 'clippath')
    w = image.width
    h = image.height
    path = [
        # The array of bezier points for the path.
        # You can modify this for your use-case.
        # This one draws a rectangle 10px from each side.
        # Format: control1-x, control1-y, center-x, center-y, control2-x, control2-y
        100, 100, 100, 100, 100, 100,
        w - 100, 100, w - 100, 100, w - 100, 100,
        w - 100, h - 100, w - 100, h - 100, w - 100, h - 100,
        100, h - 100, 100, h - 100, 100, h - 100
    ] 
    pdb.gimp_vectors_stroke_new_from_points(vectors, 0, len(path), path, True)
    pdb.gimp_image_add_vectors(image, vectors, 0)
    drawable = pdb.gimp_image_get_active_layer(image)
    pdb.file_tiff_save(image, drawable, outfile, 'image.tif', 0)

args = [(PF_STRING, 'infile', 'GlobPattern', '*.*'), (PF_STRING, 'outfile', 'GlobPattern', '*.*')]
register('python-add-path', '', '', '', '', '', '', '', args, [], add_path)

main()

Solution

  • See pdb.plug_in_sel2path(image, drawable) (drawable can be None) or pdb.plug_in_sel2path_advanced(...) (better use the first one for you sanity).

    They don't return anything but the created path is the first one (image.vectors[0]).