GIMP 在 Python-Fu 中新添加 RGBA 图层,无法在源图像后面添加新图层以使其透明

问题描述 投票:0回答:1

我有几张图像正在尝试使用 GIMP Python-Fu 来处理。我想做的是:

  1. 打开一个透明的*.png
  2. 创建一个新图层透明并将其注入现有图像下方
  3. 复制原始图层的内容并将其粘贴到新图层中
  4. 降低副本的饱和度并将其亮度设置为 0
    • 想要的效果:得到原图100%黑色的效果,成为阴影
    • Hue-Chroma 是我的手动方法,但 GIMP 的程序中似乎缺少该方法
  5. 调整背景阴影层,使其偏移 X 和 Y 量
  6. 将阴影的 Alpha/不透明度设置为 50% 透明
  7. 渲染这些层
  8. 坚持到新地点

我认为我已经掌握了总体要点,但我仍受困于新图层,即使是那些创建为 RGBA 的图层,始终具有当前在 GIMP 中设置的背景颜色。当我展平这两层时,生成的 *.PNG 填充的是背景颜色,而不是我所期望的透明度,因为 GIMP UI 具有相应的行为。

这是我所拥有的:

import os, glob, sys, time
from gimpfu import *
import gimpcolor

def ie_inventory_shadow(file, x_offset, y_offset, outputFolder):
    #Open the file; size to 64x64; duplicate layer; make black; offset 4, 5; set opacity to 50%; save to output

    print "Opening '" + file + "' ..."

    # Indicates that the process has started.
    gimp.progress_init("Opening '" + file + "' ...")

    try:
        filename = os.path.basename(file)
        # Use slicing to remove the extension
        filename_without_ext = filename[:filename.rindex('.')]


        # Open file.
        fileImage = None
        fileImage = pdb.gimp_file_load(file, file, run_mode=RUN_NONINTERACTIVE)
        print "Opened '" + filename_without_ext + "' ..."

        #resize
        pdb.gimp_context_set_interpolation(2) #INTERPOLATION-CUBIC (2)
        pdb.gimp_image_scale(fileImage, 64, 64)
        layer = fileImage.layers[0]
        print "Resized '" + filename_without_ext + "' ..."

        # Create new layer.
        pdb.gimp_context_set_background((0,255,0))
        #newLayer = pdb.gimp_layer_new(fileImage, layer.width, layer.height, RGBA_IMAGE, "Shadow", layer.opacity, layer.mode) #RGBA-IMAGE (1)
        #pdb.gimp_layer_add_alpha(newLayer)
        #pdb.gimp_drawable_fill(newLayer, 3) #FILL-TRANSPARENT (3)
        pdb.gimp_layer_new_from_visible(fileImage, fileImage, "shadow")
        print "Created New Layer for '" + filename_without_ext + "' ..."
        pdb.gimp_image_insert_layer(fileImage, newLayer, None, +1) # the +1 adds it behind the top layer
        #pdb.gimp_edit_clear(newLayer)
        ##### Fuck my life. The new layer ALWAYS has the background color. But I simply CANNOT figure out how to make this motherfucker transparent.
        print "Inserted New Layer for '" + filename_without_ext + "' ..."

        # Put image into the new layer.
        pdb.gimp_edit_copy(layer)
        floating = pdb.gimp_edit_paste(newLayer, True)

        # Update the new layer.
        newLayer.flush()
        newLayer.merge_shadow(True)
        newLayer.update(0, 0, newLayer.width, newLayer.height)
        # floating to layer
        pdb.gimp_floating_sel_to_layer(floating)
        print "Duplicated '" + filename_without_ext + "' ..."

        #make layer black
        pdb.gimp_brightness_contrast(newLayer, -127, 127)
        print "Shadowed '" + filename_without_ext + "' ..."

        #move the layer
        pdb.gimp_layer_set_offsets(newLayer, x_offset, y_offset)
        print "Moved '" + filename_without_ext + "' ..."

        #set opacity
        pdb.gimp_layer_set_opacity(newLayer, 50)
        print "Set Opacity on '" + filename_without_ext + "' ..."

        #flatten
        flattened = pdb.gimp_image_flatten(fileImage)
        print "Flattened '" + filename_without_ext + "' ..."

        # Export flattened image
        target = outputFolder + "/" + filename_without_ext + "-s.png"
        print "Saving '" + target + "' ..."
        pdb.file_png_save_defaults(fileImage, flattened, target, target, run_mode=RUN_NONINTERACTIVE)
        #pdb.file_png_save2(fileImage, flattened, target, target, False, 9, True, False, False, True, True, False, False, run_mode=RUN_NONINTERACTIVE)
        print "Saved '" + target + "'!"


    except Exception as err:
        gimp.message("Unexpected error: " + str(err))



def run(directory, x_offset, y_offset, outputFolder):
        print "Run: directory : %s" % directory
        print "Run: outputFolder : %s" % outputFolder
        print "Run: x_offset : %s" % x_offset
        print "Run: y_offset : %s" % y_offset

        start=time.time()
        print "Run: Running on directory \"%s\"" % directory
        globPath = os.path.join(directory, '*.png')

        print "looking at path: %s" % globPath

        for infile in glob.glob(globPath):
                print "Run: found file : %s" % infile
                ie_inventory_shadow(infile, x_offset, y_offset, outputFolder)
        end=time.time()
        print "Finished, total processing time: %.2f seconds" % (end-start)


if __name__ == "__main__":
        print "Running as __main__ with args: %s" % sys.argv
gimp python-fu
1个回答
0
投票

我花了两天的时间来解决这个问题,并在发布后几分钟就发现了......

问题在于扁平化调用。文档指出,展平将删除 alpha 通道:

gimp-图像-压平

[...]

不可见层被丢弃,并且生成的图像被剥离其 Alpha 通道。

解决方案是使用

gimp_image_merge_visible_layers
:

 #flattened = pdb.gimp_image_flatten(fileImage) # Unintended result
 merged = pdb.gimp_image_merge_visible_layers(fileImage, 0) #EXPAND-AS-NECESSARY (0)
© www.soinside.com 2019 - 2024. All rights reserved.