Gimp-Forum.net
PyGimp;Change color;Bucket fill selected - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: PyGimp;Change color;Bucket fill selected (/Thread-PyGimp-Change-color-Bucket-fill-selected)



PyGimp;Change color;Bucket fill selected - FloppaDisk - 03-15-2020

Hi. Trying to change the color of the bucket fill function in python gimp, but due to my inexperience with python and the structure between gimp and python i have not been successful.
Information im looking for is:
  How to swap the pre-selected colors
  How to set new color
  How to fill and selected area
  How to select and area

All of which needs to take place in pyGimp

The bellow code is what i have so far, but the result is what i expect.
Expected result should be an grid (almost like a chess board) where pyGimp color individual squares in one of the two colors (foreground and background)

#Select and square
pdb.gimp_rect_select(img, gPosX+pX, gPosY+pY, wid, hig, CHANNEL_OP_REPLACE, False, 0.0)
#Fill the square
draw.fill(FILL_FOREGROUND)


RE: PyGimp;Change color;Bucket fill selected - Ofnuts - 03-15-2020

What you want is:

Code:
➤> pdb.gimp_image_select_rectangle(img, CHANNEL_OP_REPLACE, gPosX+pX, gPosY+pY, wid, hig)
➤> pdb.gimp_drawable_edit_fill(draw,FILL_FOREGROUND)

To recap the various fill options, you can/should currently use:
  • gimp_drawable_edit_bucket_fill: simple standard bucket-fill with "similar colors", abides to both color and selection (standard GUI tool is either color or selection). Most parameters are in the gimp_context_* functions.
  • gimp_drawable_edit_fill: fill selection on drawable (simpler form of bucket-fill)
  • gimp_drawable_fill: fill whole drawable regardless of selection (used for initialization)
The gimp.Drawable.fill() method that you use is really a cover for gimp_drawable_fill, so not what you want here.

Avoid using gimp_edit_bucket_fill, gimp_edit_bucket_fill_full, gimp_drawable_edit_fill, and gimp_bucket_fill that are deprecated.


RE: PyGimp;Change color;Bucket fill selected - Ofnuts - 03-15-2020

For the colors:

To swap FG/BG colors use gimp_context_swap_colors

To set a given color you need the help of the gimpcolor module:

Code:
import gimpcolor
yellow=gimpcolor.RGB(255,255,0)
yellow=gimpcolor.RGB(1.,1.,0.)
yellow=gimpcolor.HSV(60,100,100).to_rgb()
yellow=gimpcolor.HSV(.166666,1.,1.).to_rgb(()

pdb.gimp_context_set_foreground(yellow)

Note that in these calls/constructors, when you use integers they are understood to be in the 0-255 range (or 0-360 for hues) and when you use floats they are understood as values in the 0.0-1.0 range. gimpcolors has several color definitions (RHB, HSV, HSL, CYMK) but the Gimp API wants the RGB type (hence the to_rgb() method on most).

Since you change the context, you should normally restore it before exiting the script, this is done by bracketing your script code between
gimp_context_push() and gimp_context_pop(). In practice to make sure that this happens you also bracket your code with a try/catch and restore the context after you have caught any exception. And while you are at it you can also bracket your code with undo markers so that you can undo all the script actions with a single Ctrl-Z.

The problem with catching the exception is that it masks errors and makes debugging difficult but you can retrieve the exception information to display it, so your plugin code becomes:

Code:
import traceback

def pluginFunction(image,...):
    pdb.gimp_image_undo_group_start(image)
    pdb.gimp_context_push()
    try:
        ##
        # Your actual code goes here
        ##
    except Exception as e:
        traceback.print_exc() # Terminal (Linux, OSX...)
        pdb_gimp_message(traceback.format_exc()) # Gimp error console
    pdb.gimp_context_pop()
    pdb.gimp_image_undo_group_end(image)

Or if you prefer the lighter form with Gimp objects:

Code:
import traceback

def pluginFunction(image,...):
    image.undo_group_start()
    gimp.context_push()
    try:
        ##
        # Your actual code goes here
        ##
    except Exception as e:
        traceback.print_exc() # Terminal (Linux, OSX...)
        gimp.message(traceback.format_exc()) # Gimp error console
    gimp.context_pop()
    image.undo_group_end()