首页 > 解决方案 > 如果调用某些 pdb 方法,GIMP Python-Fu 插件将不会注册

问题描述

我正在尝试使用 Python-fu 进行一些批处理,但无法让我的脚本正常工作。当我尝试调用pdb中的某些方法时,例如pdb.plug_in_nlfilt(),插件不会注册。

我在这里构建 test_batch_invert.py 示例。该示例有效并按预期处理文件,但以下代码未注册。当我有一个要处理的图像时,所有这些pdb函数在 python 控制台中单独工作。

有任何想法吗?此外,当我注释掉所有有问题的pdb调用并且插件确实注册时,它在菜单中显示为灰色,除非我打开了图像。有没有办法让它在菜单中起作用而无需手动打开另一个图像文件?

感谢大家!

-编辑:修复了一些名为“层”的拼写错误,但这并没有解决它。

#!/usr/bin/env python

# Repurposed from https://github.com/jfmdev/PythonFuSamples  (see copyright at bottom)

import os
from gimpfu import *

def process_card(img, layer, inputFolder, outputFolder):
    ''' Display the message "Hello world" in the bottom of GIMP.

    Parameters:
    img : image The current image.
    layer : layer The layer of the image that is selected.
    '''

    for file in os.listdir(inputFolder):
        try:
            # Build the full file paths.
            inputPath = inputFolder + "\\" + file
            outputPath = outputFolder + "\\" + file

            # Open the file if is a JPEG or PNG image.
            image = None
            if(file.lower().endswith(('.png'))):
                image = pdb.file_png_load(inputPath, inputPath)
            if(file.lower().endswith(('.jpeg', '.jpg'))):
                image = pdb.file_jpeg_load(inputPath, inputPath)

            # Verify if the file is an image.
            if(image != None):

                if(len(image.layers) > 0):
                    layer = image.layers[0]

                    # THIS INVERT WORKS
                    pdb.gimp_invert(layer)

                    # (image, 2 for replace, x, y, width, height)   
                    pdb.gimp_image_select_rectangle(image, 2, 865, 680, 1270, 2020)                 

                    # Including any of the rest of these pdb calls EXCEPT for pdb.file_jpg.save()
                    # will prevent plugin from registering.
                    pdb.gimp_selection_invert(image)
                    pdb.gimp_edit_clear(layer)

                    # Run alpha trimmed filter 3x
                    pdb.plug_in_nlfilt(image, layer, 1.0, 1.0, 0)
                    pdb.plug_in_nlfilt(image, layer, 1.0, 1.0, 0)
                    pdb.plug_in_nlfilt(image, layer, 1.0, 1.0, 0)

                    #run optimal smoothing once
                    pdb.plug_in_nlfilt(image, layer, 1.0, 1.0, 1)

                    #Edge enhance
                    pdb.plug_in_nlfilt(image, layer, 0.5, 0.7, 2)

                    #Select all
                    pdb.gimp_selection_all(image)

                    #zealous crop
                    pdb.plug_in_zealouscrop(image, layer)

                    #resize image
                    pdb.gimp_image_scale(image, 600, 955)

                    pdb.file_jpeg_save(image, layer, outputPath, outputPath, 0.9, 0, 0, 0, "Card", 0, 0, 0, 0)
        except Exception as err:
            gimp.message("Unexpected error: " + str(err))


register(
    "python_fu_process_card",
    "Card Scan",
    "Process scanned image with jig, output to scaled jpg",
    "My library",
    "Open source (BSD 3-clause license)",
    "2020",
    "<Toolbox>/Filters/Process Card",
    "*",
    [
        (PF_DIRNAME, "inputFolder", "Input directory", ""),
        (PF_DIRNAME, "outputFolder", "Output directory", "")
    ],
    [],
    process_card)

main()

#
# -------------------------------------------------------------------------------------
#
# Copyright (c) 2013, Jose F. Maldonado
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, 
# are permitted provided that the following conditions are met:
#
#    - Redistributions of source code must retain the above copyright notice, this 
#    list of conditions and the following disclaimer.
#    - Redistributions in binary form must reproduce the above copyright notice, 
#    this list of conditions and the following disclaimer in the documentation and/or 
#    other materials provided with the distribution.
#    - Neither the name of the author nor the names of its contributors may be used 
#    to endorse or promote products derived from this software without specific prior 
#    written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
# DAMAGE.

GIMP 详细输出提供的错误:

Querying plug-in: 'C:\Users\dried\AppData\Roaming\GIMP\2.10\plug-ins\img_BSG_card.py'
C:\Program Files\GIMP 2\bin\gimp-2.10.exe: LibGimpBase-WARNING: gimp-2.10.exe: gimp_wire_read(): error

标签: pythongimpgimpfu

解决方案


经过几次修复后,对我有用,但这些不是 PDB 调用:

  1. 您的注册声明了 2 个 args(进出目录),而您的函数需要 4 个。曾经有默认的图像和图层 args,但注册的咒语随着时间的推移发生了一些变化。image 和 layer 作为前两个 args 是专门处理的,但是如果你想要它们,你需要在注册中声明它们。自 Gimp 2.6 起,菜单位置<Toolbox>已被弃用,因此您的示例已经过时了。

  2. “\”作为文件分隔符在 Linux 和 OSX 上不起作用。“/”在任何地方都可以使用,即使在 Windows 上也是如此,但更适合使用os.path.join().

完成此操作后,脚本将为我运行(Linux 上的 Gimp 2.10.14)。我假设它会寻找具有特定特征的文件,因为如果它运行结果很奇怪:)

在您打开图像之前,菜单是灰色的,因为您的图像类型是"*"任何类型的图像,而""不是不需要任何图像的(空字符串)。

这就是说,您无需注册脚本/插件即可在批处理模式下使用它,请参见此处的示例。

在 Windows 上调试 Gimp python 脚本的一些提示。

PS:据我所知,该gimp_wire_read(): error消息可以忽略,它不是由您的脚本/插件引起的。


推荐阅读