1

我一直在试验 GIMP(v2.8.10) 层的操作,以编程方式使用自定义 gimpfu 插件。我成功地选择、旋转、缩放和平移现有图层的一部分:

插件内部的部分代码:

为相关性而简化。所有这些说明都有效。

# I only use one layer in the probe image
layer = img.layers[0]

selection = pdb.gimp_rect_select(img, init_coords[0], init_coords[1],
                                            my_width,my_height,2,0,0)

# Copy and paste selection
pdb.gimp_edit_copy(layer)
float_layer = pdb.gimp_edit_paste(layer, 1)

# Transform floating selection step by step
float_layer = pdb.gimp_item_transform_rotate_simple(float_layer, 
                                             ROTATE_90, 1, 0, 0)
float_layer = pdb.gimp_layer_scale(float_layer, new_width,new_height, 1)
float_layer = pdb.gimp_layer_translate(float_layer, h_offset, 0)

问题

  • 如果我删除最后一行(gimp_layer_translate),其他一切正常。
  • 否则,如果我删除包含的行gimp_layer_scale,其他一切都有效。
  • 但是,如果我尝试将所有这些功能一起使用,则会gimp_layer_translate崩溃RuntimeError

程序 gimp-layer-translate 已被调用,但 «layer» arg 的 ID 不正确。可能正在尝试使用不存在的层进行操作。

我不知道为什么它会这样失败。如果他们单独工作,为什么不一起工作?我想知道。

参考

我过去常常从template.pygimpbook.com开始构建插件,因为它似乎有一个坚实的结构,可以将代码包含在“撤消块”中。我的主要信息来源pdb是我在谷歌上找到的这个网站。另外,我发现了一些我在这个问题上需要的程序。

此外,我之前的一个问题可能有助于作为如何开始为 gimp 制作自定义插件的参考。

是什么让我卡住了

我不确定原因是 gimp 的错误还是我的代码执行错误。由于链接问题的答案上发布了一些代码,我想知道我是否过度使用了可用内存(毕竟,在我发现之前我不知道我必须使用特定的过程调用来销毁对象关于那个答案)。如果我不删除它们会怎样?我想知道。它们会在我关闭 GIMP 之前一直保留在 RAM 中,还是会生成会淹没 GIMP 系统的持久文件?老实说,我不知道忽视这个问题的后果。

4

2 回答 2

2

使用图层时删除分配。以下是销毁 float_layer 变量的方法:

# This creates a new layer that you assign to the float_layer
float_layer = pdb.gimp_item_transform_rotate_simple(float_layer, 
                                             ROTATE_90, 1, 0, 0)
# This does not return anything at all, it works with the layer in-place.
# But you overwrite the float_layer variable anyway, destroying it.
float_layer = pdb.gimp_layer_scale(float_layer, new_width,new_height, 1)
# This does not work because float_layer is no longer referencing the layer at all
float_layer = pdb.gimp_layer_translate(float_layer, h_offset, 0)

所以改为这样做:

float_layer = pdb.gimp_item_transform_rotate_simple(float_layer, 
                                             ROTATE_90, 1, 0, 0)
pdb.gimp_layer_scale(float_layer, new_width,new_height, 1)
pdb.gimp_layer_translate(float_layer, h_offset, 0)
于 2016-01-15T10:21:30.913 回答
0

Layer在执行这两个函数之前,我通过将 floating_layer 切换到新实例来修复它。

fixed_layer = gimp.Layer(img, 'float_layer', int(gh), int(gw),
                                           RGBA_IMAGE, 100, 1)

所以我做了以下组合:

# Transform floating selection step by step
float_layer = pdb.gimp_item_transform_rotate_simple(float_layer, 
                                             ROTATE_90, 1, 0, 0)
fixed_layer = gimp.Layer(img, 'float_layer', new_width, new_height,
                                           RGBA_IMAGE, 100, 1)
fixed_layer = pdb.gimp_layer_scale(float_layer, new_width,new_height, 1)
fixed_layer = pdb.gimp_layer_translate(float_layer, h_offset, 0)

问题似乎是,在层上执行了一些程序之后,它变成None了(就像它被自动从内存中删除......)。该解决方案允许我执行 2 或 3 个以上的操作......这不是一个完整的解决方案。我会继续研究

于 2016-01-14T17:03:34.960 回答