繁体   English   中英

gimp python-fu创建简单边框

[英]gimp python-fu create simple border

所以我正在用python做我的第一个gimp插件。 到目前为止,它正在按预期方式工作:

它首先为边框创建第二层,然后调整其大小以适合画布,然后选择活动层的轮廓(“常规” GIMP中的“ Alpha到选择”),将其边框,将活动层的轮廓删除为保留图层上的像素,然后将边框增加1,将其锐化并填充它。

但是有时似乎选择了整个图层而不是轮廓,这是GIMP错误还是我做错了?

#!/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()

您的代码对图层有一些困惑。 调用脚本时,脚本的“ drawable”参数是活动层,因此您实际上并不需要该额外的“ layer”参数,只需将“ drawable”作为您的图层(甚至可以重命名function参数)。

此外,将盒装层添加到盒装层下面要快得多/干净得多,因此无需删除任何内容(盒装层将盒装内部隐藏起来):

#!/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()

除非您真的想要圆角,否则一种更快的方法是使用盒装图层的宽度/高度加上厚度* 2来创建盒装图层,并使用图层偏移量将其放置在((-thickness,-thickness)from盒装图层偏移)。 那你就装满了...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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