我有许多不规则边界的图层和背景。我想通过在图像的边界上逐渐应用某种过滤器来将图像"融入"到背景中。有没有办法自动执行此操作?
我尝试的是:
- 合并所有图层
- 选择此组合图层的背景
- 反转选区
- 将羽化应用于所选内容 在
- 遮罩层中用白色填充选区
这种方法有点有效,但我的问题是我有重叠的层。这意味着上述方法将图层的边界淡化到背景中,而不是彼此淡化。
我从未尝试过在 GIMP 中编写脚本,但如果有人有可行的解决方案,我非常愿意尝试。
是的- 这可以是可编写脚本的。
基本上,GIMP中的脚本可以执行通常通过UI可能执行的任何操作,而有些则不能。
因此,一旦确定了需要为每个所需层执行的步骤 -
例如,按具有最大阈值的颜色选择并消除透明度 - (这应该为您提供具有图层可见内容的形状的选择)。
然后,缩小、反转和羽化选区 并应用"GIMP 编辑剪切",或者以编程方式"gimp-edit-fill",带有"TRASPARENT_FILL"。 对于这些操作中的每一个,您都可以在help->procedure_browser
下检查可用的调用。
现在,要创建一个 GIMP Python 脚本,你需要做的是:
创建一个Python 2程序,该程序将从"gimpfu"模块导入所有内容;将其作为可执行文件放置在GIMP中s plug-in folder (check the folders at
edit->preferences->folder'
在脚本中,您编写主函数和任何其他函数 - 主函数可以将任何 GIMP 对象作为输入参数,如图像、可绘制对象、颜色、调色板、r 简单的字符串和您想要的整数 -
之后,您对寄存器gimpfu.register
函数进行适当的调用 - 这将使您的脚本成为插件,并在 GIMP 中使用自己的菜单选项。通过调用 gimpfu.main() 来完成脚本。
此外,没有"现成"的方法可以在插件中选择一组图层,而不是仅将当前活动的图层作为输入。 作为这些情况的一个非常方便的解决方法,我滥用了"链接"图层标记(单击图层的对话框,在可见性图标的右侧将显示一个"链"图标,指示图层已链接)
总而言之,插件的模板只是:
#! /usr/bin/env python
# coding: utf-8
import gimp
from gimpfu import *
def recurse_blend(img, root, amount):
if hasattr(root, "layers"):
# is image or layer group
for layer in root.layers:
recurse_blend(img, layer, amount)
return
layer = root
if not layer.linked:
return # Ignore layers not marked as "linked" in the UI.
# Perform the actual actions:
pdb.gimp_image_select_color(img, CHANNEL_OP_REPLACE, layer, (0,0,0))
pdb.gimp_selection_shrink(img, amount)
pdb.gimp_selection_invert(img)
pdb.gimp_selection_feather(img, amount * 2)
pdb.gimp_edit_clear(layer)
def blend_layers(img, drawable, amount):
# Ignore drawable (active layer or channel on GIMP)
# and loop recursively through all layers
pdb.gimp_image_undo_group_start(img)
pdb.gimp_context_push()
try:
# Change the selection-by-color options
pdb.gimp_context_set_sample_threshold(1)
pdb.gimp_context_set_sample_transparent(False)
pdb.gimp_context_set_sample_criterion(SELECT_CRITERION_COMPOSITE)
recurse_blend(img, img, amount)
finally:
# Try to restore image's undo state, even in the case
# of a failure in the Python statements.
pdb.gimp_context_pop() # restores context
pdb.gimp_selection_none(img)
pdb.gimp_image_undo_group_end(img)
register(
"image_blend_linked_layers_edges", # internal procedure name
"Blends selected layers edges", # Name being displayed on the UI
"Blend the edges of each layer to the background",
"João S. O. Bueno", # author
"João S. O. Bueno", # copyright holder
"2018", # copyright year(s)
"Belnd layers edges", # Text for menu options
"*", # available for all types of images
[
(PF_IMAGE, "image", "Input image", None), # Takes active image as input
(PF_DRAWABLE, "drawable", "Input drawable", None), # takes active layer as input
(PF_INT, "Amount", "amount to smooth at layers edges", 5), # prompts user for integer value (default 5)
],
[], # no output values
blend_layers, # main function, that works as entry points
menu="<Image>/Filters/Layers", # Plug-in domain (<Image>) followed by Menu position.
)
main()