pythongimpgimpfu

GIMP Python-Fu plugin will not register if some pdb methods are called


I'm trying to do some batch processing with Python-fu and just can't get my script to work. When I try to call some methods in pdb, such as pdb.plug_in_nlfilt(), the plug in won't register.

I'm building off the test_batch_invert.py example here. That example works and it processes files as intended, but the code below does not register. All these pdb functions work individually in the python console when I have an image up to process.

Any ideas? Also, when I comment out all the problematic pdb calls and the plugin does register, it shows up grayed out in the menu unless I have an image open. Is there a way to make it functional in the menu without having to manually open another image file?

Thanks everyone!

-- EDIT: Fixed some typos with the name 'layer' but that did not solve it.

#!/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.

Error provided by GIMP verbose output:

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

Solution

  • Works for me, sort of, after a couple of fixes but these aren't the PDB calls:

    1. Your registration declares 2 args (in & out directories) and your function takes 4. There used to be default image and layer args, but the registration mantras have changed a bit over time. Image and layer as the first two args are processed specifically, but if you want them you need to declare them in the registration. The menu location <Toolbox> is deprecated since Gimp 2.6 so your example is vastly outdated.

    2. The "\" as a file separator doesn't work on Linux and OSX. "/" works everywhere, even on Windows, but it is more proper to use os.path.join().

    Once this is done the script runs for me (Gimp 2.10.14 on Linux). I assume it looks for file with specific characteristics because if it runs the results are strange :)

    The menu is grayed out until you open an image because your image type is "*" which is an image of any type, instead of "" (empty string) that doesn't need any image.

    This said, you don't need to register the script/plugin to use it in batch mode, see here for an example.

    Some hints to debug Gimp python scripts on Windows here.

    PS: as far as I know, the gimp_wire_read(): error message can be ignored, it it not caused by your script/plugin.