简体   繁体   中英

gimp python-fu create simple border

So I'm making my first gimp plugin with python. So far it is working as it should:

It first creates a secondary layer for the border, then it resizes it to fit the canvas, then it selects the outline of the active layer (Alpha to Selection in "normal" GIMP), borders it, removes the outline of the active layer to preserve pixels on the layer, then it grows the border by 1, sharpens it and fills it.

But sometimes it seems to select the whole layer instead of the outline of it, is this a GIMP bug, or did I do something wrong?

#!/usr/bin/env python

from gimpfu import *
import time

def white_box(img, drawable, thickness = 5, layer = None):

    pdb.plug_in_autocrop_layer(img, drawable)

    # layer = pdb.gimp_image_get_active_layer(img)  
    box = pdb.gimp_layer_new(img, 1, 1, 1, pdb.gimp_item_get_name(layer) + "_white box", 100, 0)
    pdb.gimp_image_insert_layer(img, box, None, -1)

    pdb.gimp_layer_resize_to_image_size(box)
    # pdb.gimp_layer_resize_to_image_size(layer)

    pdb.gimp_image_select_item(img, 0, layer)
    pdb.gimp_message("Selected layer")
    # pdb.gimp_edit_fill(drawable, 2)
    time.sleep(1)


    pdb.gimp_selection_border(img, thickness)
    pdb.gimp_message("Selected a border with a thickness of " + str(thickness))
    time.sleep(1)

    pdb.gimp_image_select_item(img, 1, layer)
    pdb.gimp_message("Removed layer from selection")
    time.sleep(1)

    pdb.gimp_selection_grow(img, 1) 
    pdb.gimp_message("Grew selection")
    time.sleep(1)

    pdb.gimp_selection_sharpen(img)
    pdb.gimp_message("Sharpened")
    time.sleep(1)

    pdb.gimp_image_set_active_layer(img, box)

    pdb.gimp_edit_fill(pdb.gimp_image_get_active_drawable(img), 2)

    pdb.plug_in_autocrop_layer(img, drawable)
    pdb.plug_in_autocrop_layer(img, pdb.gimp_image_get_active_drawable(img))

register(
    "python_fu_white_box",
    "Create White Box",
    "Create a white box around active layer",
    "Misha Smit",
    "Misha Smit",
    "2018",
    "White Box...",
    "RGBA",      # Create a new image, don't work on an existing one
    [
        (PF_IMAGE, "img", "Image", None),
        (PF_DRAWABLE, "drawable", "Drawable", None),
        (PF_INT, "thickness", "Box Thickness", 5),
        (PF_LAYER, "layer", "LAYER:", None)
    ],
    [],
    white_box, menu="<Image>/Filters/Render")

main()

Your code shows a bit of confusion about layers. The "drawable" parameter of the script is the active layer when the script is called, so you don't really need that extra "layer" parameter, consider "drawable" as your layer (you can even rename the function parameter).

In addition, it is much faster/cleaner to add the box layer under your boxed layer, so no need to remove anything (the boxed layer hides the inside of the box one):

#!/usr/bin/env python

from gimpfu import *
import time

def white_box(img, layer, thickness = 5):

    pdb.plug_in_autocrop_layer(img, layer)

    # Create the box layer with the image size
    box = pdb.gimp_layer_new(img, img.width,img.height, RGBA_IMAGE, layer.name + "_white box", 100, NORMAL_MODE)
    # Insert it **below** the boxed layer
    pdb.gimp_image_insert_layer(img, box, None, 1+pdb.gimp_image_get_item_position(img, layer)) 
    pdb.gimp_image_select_item(img, 0, layer)
    print("Selected layer")

    pdb.gimp_selection_grow(img, thickness)

    #pdb.gimp_selection_sharpen(img)
    #print("Sharpened")

    pdb.gimp_edit_fill(box, 2)

    pdb.plug_in_autocrop_layer(img,box)

register(
    "python_fu_white_box",
    "Create White Box",
    "Create a white box around active layer",
    "Misha Smit",
    "Misha Smit",
    "2018",
    "White Box...",
    "RGBA",     
    [
        (PF_IMAGE, "img", "Image", None),
        (PF_DRAWABLE, "drawable", "Drawable", None),
        (PF_INT, "thickness", "Box Thickness", 5),
    ],
    [],
    white_box, menu="<Image>/Test/")

main()

Unless you really want the round corners, an even faster way is to create the box layer with the width/height of the boxed layer plus thickness*2, and use the layer offsets to position it ((-thickness,-thickness) from the boxed layer offsets). Then you bucket fill...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM