Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using files path as variables from .bat
#1
Hello,
I have a problem with script in Python-FU. I want to run the script by drag-and-drop two files on .bat file.
Script is working fine when I run it from GIMP menu, but from .bat i receive error:
Code:
Error: Invalid number of arguments for python-fu-file-import-as-layer (expected 5 but received 2)

.bat file:
Code:
@echo off
setlocal enabledelayedexpansion

if "%~2"=="" (
   echo Please provide two file paths.
   pause
   exit /b
)
set "file1=%~1"
set "file2=%~2"
start "" "C:\Program Files\GIMP 2\bin\gimp-2.10.exe" -b "(python-fu-file-import-as-layer \"%file1%\" \"%file2%\")"

exit /b


file-import-as-layer.py
Code:
#!/usr/bin/env python2
from gimpfu import *

def file_import_as_layer(fname_one, fname_two, image, layer):

   try:
       # Load the main image
       image = pdb.gimp_file_load(fname_one, fname_one)

       # Check if the image loaded successfully
       if not image:
           raise Exception("Failed to load the main image.")

       # Load the second image as a layer
       drawable2 = pdb.gimp_file_load_layer(image, fname_two)

       # Check if the layer loaded successfully
       if not drawable2:
           raise Exception("Failed to load the second image as a layer.")

       # Make sure the second layer is visible
       pdb.gimp_item_set_visible(drawable2, True)

       # Get the layer position in the layer stack
       position = len(image.layers)

       # Create a new image for both layers
       new_image = pdb.gimp_image_new(image.width, image.height, image.base_type)

       # Duplicate the main layer
       new_layer_main = pdb.gimp_layer_new_from_drawable(image.layers[0], new_image)
       
       # Duplicate the second layer
       new_layer_inserted = pdb.gimp_layer_new_from_drawable(drawable2, new_image)
       
       # Insert the main layer into the new image
       pdb.gimp_image_insert_layer(new_image, new_layer_main, None, 0)

       # Insert the second layer into the new image
       pdb.gimp_image_insert_layer(new_image, new_layer_inserted, None, 1)

       # Show the new image with both layers
       gimp.Display(new_image)

   except Exception as e:
       pdb.gimp_message("Error: {}".format(str(e)))

# Register the Python-fu script
register(
   "python_fu_file_import_as_layer",
   "Import a file as a layer",
   "Import a file as a layer into the current image",
   "Author",
   "Author",
   "2024",
   "<Image>/File/Import As Layer...",
   "*",
   [
       (PF_STRING, "fname_one", "Main Image", ""),
       (PF_STRING, "fname_two", "Image to import as layer", "")
   ],
   [],
   file_import_as_layer
)

main()


What arguments I need to specify?
Reply
#2
(01-14-2024, 03:02 PM)DeX Wrote: Hello,
I have a problem with script in Python-FU. I want to run the script by drag-and-drop two files on .bat file.
Script is working fine when I run it from GIMP menu, but from .bat i receive error:
Code:
Error: Invalid number of arguments for python-fu-file-import-as-layer (expected 5 but received 2)

.bat file:
Code:
@echo off
setlocal enabledelayedexpansion

if "%~2"=="" (
   echo Please provide two file paths.
   pause
   exit /b
)
set "file1=%~1"
set "file2=%~2"
start "" "C:\Program Files\GIMP 2\bin\gimp-2.10.exe" -b "(python-fu-file-import-as-layer \"%file1%\" \"%file2%\")"

exit /b


file-import-as-layer.py
Code:
#!/usr/bin/env python2
from gimpfu import *

def file_import_as_layer(fname_one, fname_two, image, layer):

   try:
       # Load the main image
       image = pdb.gimp_file_load(fname_one, fname_one)

       # Check if the image loaded successfully
       if not image:
           raise Exception("Failed to load the main image.")

       # Load the second image as a layer
       drawable2 = pdb.gimp_file_load_layer(image, fname_two)

       # Check if the layer loaded successfully
       if not drawable2:
           raise Exception("Failed to load the second image as a layer.")

       # Make sure the second layer is visible
       pdb.gimp_item_set_visible(drawable2, True)

       # Get the layer position in the layer stack
       position = len(image.layers)

       # Create a new image for both layers
       new_image = pdb.gimp_image_new(image.width, image.height, image.base_type)

       # Duplicate the main layer
       new_layer_main = pdb.gimp_layer_new_from_drawable(image.layers[0], new_image)
       
       # Duplicate the second layer
       new_layer_inserted = pdb.gimp_layer_new_from_drawable(drawable2, new_image)
       
       # Insert the main layer into the new image
       pdb.gimp_image_insert_layer(new_image, new_layer_main, None, 0)

       # Insert the second layer into the new image
       pdb.gimp_image_insert_layer(new_image, new_layer_inserted, None, 1)

       # Show the new image with both layers
       gimp.Display(new_image)

   except Exception as e:
       pdb.gimp_message("Error: {}".format(str(e)))

# Register the Python-fu script
register(
   "python_fu_file_import_as_layer",
   "Import a file as a layer",
   "Import a file as a layer into the current image",
   "Author",
   "Author",
   "2024",
   "<Image>/File/Import As Layer...",
   "*",
   [
       (PF_STRING, "fname_one", "Main Image", ""),
       (PF_STRING, "fname_two", "Image to import as layer", "")
   ],
   [],
   file_import_as_layer
)

main()


What arguments I need to specify?

As written your script takes at least 4 args, the two file names plus an image and a layer. I don't know what these are for, but if you change the image type to an empty string ('' instead of '*') it will tell Gimp that you don't need an image to start with, and so you should be able to drop the image and layer arguments.
You should also use the new form of registration:

Code:
# Register the Python-fu script
register(
   "python_fu_file_import_as_layer",
   "Import a file as a layer",
   "Import a file as a layer into the current image",
   "Author",
   "Author",
   "2024",
   "Import As Layer...",    ##### Only the menu entry, menu position below
   "",                      ##### No image type
   [
       (PF_STRING, "fname_one", "Main Image", ""),
       (PF_STRING, "fname_two", "Image to import as layer", "")
   ],
   [],
   file_import_as_layer,
   menu="<Image>/File/"   ##### Menu position in "menu" named argument
)

I'm very confused by your code, why is is creating an image from the first file, and then creating a second image and copying the layers to it? And if yo uinstist on doing that unnecessary copy, at least delete the first image.

The syntax you use attempts to call the script from Scheme and not from Python which I suspect is where the 5th argument comes from (the run-mode is optional in Python). If you want to use to this syntax, you may have to use a RUN-NONINTERACTIVE (or just 1) as a first parameter, followed by the two file names. A goo dwayt to find out is to start the Script-fu (not python-fu) console, hit the Browse button, and search your plugin. You will see how many parameters are expected when called from script-fu and what they are.
Reply


Forum Jump: