Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gimp 3.0.4 python batch interpreter oddities
#5
(08-10-2025, 08:22 AM)Ofnuts Wrote: I don't think there is a solution by just changing the form of the python code (or at least it depends what your actual code wants to do, there is no general solution, and there maybe no solution to many cases). A technique I use since 2.x is to have the -b run import some_code; some_code.execute() and have a separate some_code python module (a bit clumsy but works in Windows). In Linux, you can stuff the python code, newlines and all, in a variable with a "heredoc" and use that variable with the -b

Code:
#! /bin/bash

# Read python code into a variable
# -d '' below necessary to keep the newlines.
read -r -d '' python <<'EOF'
gi.require_version('Gimp', '3.0')
from gi.repository import Gimp

print(f"Running batch in Gimp {Gimp.version()}")  # Just shows that we imported Gimp correctly

if True:
   print("This works!!!")
EOF

# Call Gimp passing the variable's contents in a `-b` argument
gimp -idf --batch-interpreter=python-fu-eval -b "$python" --quit

Thanks for your response, Offnuts. I had imagined that the batch-interpreter would interpret consecutive commands as though they were typed within the Gimp Python console, but that is obviously not the case. I usually want to run a python script with input arguments and one simple work-around I have used is, for example,

~/AppImages/gimp/GIMP-3.0.4-x86_64.AppImage -i --quit --batch-interpreter=python-fu-eval -b 'with open("path_to_prog.py") as fid:    arg1,arg2,arg3=1,2,"/home/user/.config/GIMP/3.0/progs/prog.py";   exec(fid.read())'

where in this case three example arguments are defined. The called program prog.py can use these arguments directly as they are global. This little prog.py

#! /usr/bin/env python3
#
import gi
gi.require_version("Gimp","3.0")
from gi.repository import Gimp
#
print(f"These are the arguments {arg1}, {arg2}, {arg3}.")

would print the arguments. The declaration of the arguments has to come after the "with" statement for the reasons discussed above. The arguments could be defined within a shell script if they need to be determined programatically e.g.

#! /bin/sh
#
pathtoprog="\"/home/user/.config/GIMP/3.0/progs/prog.py\""
arg1=1
arg2=2
arg3="\"/home/user/GIMP/3.0/configfile.txt\""
#
~/AppImages/gimp/GIMP-3.0.4-x86_64.AppImage -i --quit --batch-interpreter=python-fu-eval -b "with open($pathtoprog) as fid:   arg1,arg2,arg3=$arg1,$arg2,$arg3;   exec(fid.read())"

This is a bit messy. Is there a neater way to do it?
Reply


Messages In This Thread
RE: Gimp 3.0.4 python batch interpreter oddities - by Andrew_L - 08-10-2025, 08:06 PM

Forum Jump: