Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyGimp;Change color;Bucket fill selected
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
What you want is:

➤> 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.
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:

import gimpcolor


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:

import traceback

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

Or if you prefer the lighter form with Gimp objects:

import traceback

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

Forum Jump: