Python-fu:如何批处理颜色到 alpha 和不透明度阈值

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

我正在尝试使用 Python-Fu 脚本在 GIMP 2.10 中批处理图像。该脚本应对文件夹中的每个图像应用以下转换:

  • 颜色到具有 RGB 值的 alpha (246, 246, 246)
  • 透明度阈值为 0.035
  • 不透明度阈值为 1.000 处理后的图像应保存到不同的文件夹中。

我正在使用以下代码:

#!/usr/bin/env python

from gimpfu import *
import os

def batch_process_color_to_alpha_opacity_threshold(input_folder, output_folder):
    """
    Batch process images in input_folder and save them in output_folder after applying color to alpha and opacity threshold
    """
    # Make sure output folder exists
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # Loop over files in input folder
    for filename in os.listdir(input_folder):
        # Open the image
        input_path = os.path.join(input_folder, filename)
        image = pdb.gimp_file_load(input_path, input_path)

        # Get the layer
        layer = pdb.gimp_image_get_active_layer(image)

        # Apply color to alpha
        pdb.gimp_layer_add_alpha(layer)
        pdb.gimp_color_to_alpha(layer, (246,246,246))

        # Apply transparency threshold
        pdb.plug_in_transparency_threshold(image, layer, 0.035, 0.0, 1.0)

        # Apply opacity threshold
        pdb.plug_in_opacity_threshold(image, layer, 1.0, 0.0, 1.0)

        # Save the processed image
        output_path = os.path.join(output_folder, filename)
        pdb.gimp_file_save(image, layer, output_path, output_path)

        # Close the image
        pdb.gimp_image_delete(image)

    pdb.gimp_quit(0)

# Register the script with GIMP
register(
    "python_fu_batch_process_color_to_alpha_opacity_threshold",
    "Batch process images with color to alpha and opacity threshold",
    "Batch process images in a folder with color to alpha and opacity threshold, and save them in a different folder.",
    "Author Name",
    "Author Name",
    "2023",
    "<Toolbox>/Filters/Batch Process Color to Alpha and Opacity Threshold",
    "*",
    [
        (PF_DIRNAME, "input_folder", "Input folder", "C:/folder"),
        (PF_DIRNAME, "output_folder", "Output folder", "C:/folder2"),
    ],
    [],
    batch_process_color_to_alpha_opacity_threshold,
)

# Start the script
main()

我将包含上述代码的python文件放入GIMP插件中,并在GIMP的python控制台中应用脚本后,我在控制台中自动得到了这一行: pdb.python_fu_batch_process_color_to_alpha_opacity_threshold(输入文件夹,输出文件夹))

但是出现这个错误:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
RuntimeError: execution error

同时出现弹窗:

Calling error for procedure 'gimp-procedural-db-proc-info':
Procedure 'gimp-color-to-alpha' not found

我已经检查过 GIMP Python 插件目录是否设置正确。我还运行了一个不同的 GIMP Python 插件来查看问题是否特定于我的脚本,并且它工作正常。

有人可以帮我弄清楚我的代码有什么问题吗?

batch-processing opacity gimp alpha-transparency python-fu
© www.soinside.com 2019 - 2024. All rights reserved.