12-10-2022, 07:39 AM
(12-09-2022, 03:43 PM)Mate de Vita Wrote: So this is how far I've gotten using the GIMP docs and a ton of googling:
I find my files in a python script and then use the following code to call a separate GIMP script on each of the files:
The gimp.py file then contains the following code:Code:
for f in found_files:
scriptpath = r'path/to/script'
scriptname = 'gimp.py'
pycode = f'import sys; sys.path.insert(0, "{scriptpath}"); import {scriptname}; {scriptname}.run({f})'
subprocess.run(f'gimp -idf --batch-interpreter python-fu-eval -b \'{pycode}\' -b \'pdb.gimp_quit(1)\'', shell=True, check=True)
Code:
from gimpfu import *
import gimpcolor
def run(f):
xcf = pdb.gimp_xcf_load(0, f, f)
for layer in xcf.layers:
layer.opacity = 100.
if layer.name == 'Currently Green':
pdb.gimp_context_set_foreground(gimpcolor.RGB(255, 0, 0))
layer.fill(FOREGROUND_FILL)
elif layer.name == 'Currently Red':
pdb.gimp_context_set_foreground(gimpcolor.RGB(0, 255, 0))
layer.fill(FOREGROUND_FILL)
pdb.gimp_xcf_save(0, xcf, None, '/path/to/save/to.xcf', '/path/to/save/to.xcf')
xcf.merge_visible_layers(NORMAL_MODE)
pdb.file_png_save(xcf, xcf.layers[0], '/export/path.png', '/export/path.png', 0, 9, 1, 0, 0, 1, 1)
However this results in a bunch of batch command experienced an execution error error messages, which I don't know how to debug, since printing doesn't seem to work from the gimp script so I can't even tell where the error is happening.
What would be the correct way to fill the layers with the desired colours?
Several things:
- Your "external" script in PythonV3. Keep in mind that the Python using by Gimp is PythonV2 (so that's the old print syntax and there are no f-strings). If you abide to this then print statements do send output to the Gimp process StdOut and on Linux/OSX if you start Gimp from a terminal this is readily readable. Things are a bit more complicated on windows but there are ways.
- You also have to make sure that you have Python support in Gimp (presence of Filters ➤ Python-fu ➤ Console). Not too clear on which OS you are but on Windows installing both Gimp and an external Python interpreter can mess up things for Gimp.
- Using the shell=True form is somewhat risky, given the nesting of quotes you have to deal with.
- You are importing moduleName.py, not moduleName
- NORMAL_MODE is not really valid for merge_visible_layers(). That should be EXPAND_AS_NECESSARY (same value, but better vibes)
- If would be a lot better to pass a directory to scan for Gimp and to iterate the files inside the Gimp script. With the current form you are potentially paying the price of a Gimp startup for every file (unless there is already a Gimp instance running)