GIMP Python - Fill Path/Vector with color

1.1k views Asked by At

I am trying to develop a script that i can run on opened SVG files. I want to iterate over all paths and fill the path with an arbitrary color ( I will be replacing this part of the code later). The first stage of this is just iterating over the paths, and I cannot seem to figure out how to do this. My code is below - why am I not seeing any paths being iterated over?

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gimpfu import *

def plugin_main(image, layer, path):
    vectors_count, vectors = pdb.gimp_image_get_vectors(image)
    for n in vectors:
        pdb.gimp_image_select_item(image,CHANNEL-OP-REPLACE,n)
        foreground = pdb.gimp_context_get_foreground()
        pdb.gimp_edit_fill(image.layers[0], foreground)

register(
    "create_polygon_art",
    "Fills all the paths with the average color within path",
    "Fills all the paths with the average color within path",
    "Bryton Pilling",
    "Bryton Pilling",
    "2018",
    "<Image>/Filters/Fill all paths with average color",
    "RGB*, GRAY*",
    [],
    [],
    plugin_main
)

main()

I have also tried a number of different approaches I have found by googling, including using something simpler for the iteration like:

for v in gimp.Vectors

But no matter what I try I cannot seem to get evidence of an iteration over the paths.

I am using gimp 2.10.6 on Windows 10 64-bit.

1

There are 1 answers

1
xenoid On BEST ANSWER

It's a trap... pdb.gimp_image_get_vectors(image) returns a list of integer ID for the paths, but the later calls require a gimp.Vectors object.

image.vectors is indeed a list of gimp.Vectors and you can iterate all the paths with

for vector in image.vectors:

More problems:

  • You declare two args in register() but have three in your function. In practice you don't need the path argument, since you aere going to iterate them all anyway.
  • The layer argument of your function is the active layer when the plugin is called, and is normally the one you want to paint
  • gimp-edit-fill takes a color source and not a color. When you go further with your code you will have to set the foreground color, and push/pop the context
  • CHANNEL-OP-REPLACE isn't a valid Python symbol, in Python you should use CHANNEL_OP_REPLACE (with underscores)

Two collections of python scripts here and there.

If you are under Windows, some hints to debug your scripts here

Your code with fixes:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gimpfu import *

def plugin_main(image, layer):
    for p in image.vectors:
        pdb.gimp_image_select_item(image,CHANNEL_OP_REPLACE,p)
        pdb.gimp_edit_fill(layer, FOREGROUND_FILL)

register(
    "create_polygon_art",
    "Fills all the paths with the average color within path",
    "Fills all the paths with the average color within path",
    "Bryton Pilling",
    "Bryton Pilling",
    "2018",
    "<Image>/Test/Fill all paths with average color",
    "RGB*, GRAY*",
    [],
    [],
    plugin_main
)

main()

You can make you code more user-friendly by painting "strokes" (so you have one path with several strokes). If you want individual selections on strokes, you can copy them to a temporary path. Code for this can be found in some scripts in the collections above.