Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Programmatic XCF text layer editing
#1
[Image: Perfect-Fit.png]
The above was the answer to a question I found in reddit thanks to a similar quest of mine. I'm set out to do just that, and my lackings on Python programming are very nicely being covered by ChatGPT.

I have text layers in an xcf file with descriptive names and at this point, all I want to do is replace the text in them programmatically from a python script..

Chat GPT has already come up with some python code but it's throwing up a crucial error right at the beginning:
Code:
import gimpfu

line 1, in <module> import gimpfu ModuleNotFoundError: No module named 'gimpfu'

Then Chat GPT suggested this:
Code:
import sys
sys.path.append("/usr/lib/gimp/2.0/python")
import gimpfu

Same error pretty much:
line 3, in <module> import gimpfu ModuleNotFoundError: No module named 'gimpfu'

Chat GPT suggested to find the location for gimpfu.py

Code:
find / -name gimpfu.py 2>/dev/null

That rendered nothing in my terminal. I'm on bullseye, MX Linux, and GIMP is installed from the repository. I can reinstall should that be the suggestion. I'm on GIMP 2.10.28


Any ideas as to how to go about this? Are there any sample files that could show the editing of an existing text laxer? But then, How can I get python to import the library that talks to the GIMPs API?
Reply
#2
gimpfu is only "visible"  when the script is run from Gimp (either as a plugin or as a batch). So you don't have to tweak paths to be able to import it. In fact, I do run scripts "raw" form the command line, and I know that when I get to the import gimpfu error there are no remaining big syntax errors like unmatched parentheses or quotes or code indentation problems and I can try my luck with Gimp.

One problem you may have however in 2023 is that Gimp still relies on PythonV2, which has been deprecated in 2020, so 1) it is not always installed by default in your distro, and 2) when it is installed (by default or explicitly), the Gimp bit that enables Python is not. All isn't lost however, there are several ways to run Python scripts in Gimp on recent distros.

Then it is just a matter of traversing the layers, using pdb.gimp_text_layer_get_text(layer) and pdb.gimp_text_layer_set_text(layer, text).

I do have a script that does something similar (changes the font instead of the text), see ofn-replace-font here.
Reply
#3
(02-11-2023, 05:43 PM)Ofnuts Wrote: gimpfu is only "visible"  when the script is run from Gimp (either as a plugin or as a batch). So you don't have to tweak paths to be able to import it. In fact, I do run scripts "raw" form the command line, and I know that when I get to the import gimpfu error there are no remaining big syntax errors like unmatched parentheses or quotes or code indentation problems and I can try my luck with Gimp.

Thanks so much for your reply bud. Do I then understand correctly that even using python2 (which I just installed) there's no way to run a python script straight from the terminal without having to open the entire Gimp interface to run the script inside of it?

(02-11-2023, 09:47 PM)Anthony Buff Wrote:
(02-11-2023, 05:43 PM)Ofnuts Wrote: gimpfu is only "visible"  when the script is run from Gimp (either as a plugin or as a batch). So you don't have to tweak paths to be able to import it. In fact, I do run scripts "raw" form the command line, and I know that when I get to the import gimpfu error there are no remaining big syntax errors like unmatched parentheses or quotes or code indentation problems and I can try my luck with Gimp.

Thanks so much for your reply bud. Do I then understand correctly that even using python2 (which I just installed) there's no way to run a python script straight from the terminal without having to open the entire Gimp interface to run the script inside of it?

I'm trying to wrap my head about your reply (and taking it with zoloft and any other anti-depresant) as I thought the entire script approach was a way to manipulate XCF files using gimp without touching its user interface, like all in the background type of thing
Reply
#4
You don't need to open the UI(*), you can use Gimp+Python in batch mode from a shell (or shell script), but your script still has to start a Gimp process and tell it to run your Python. See this example.

(*) Although another option is to write a plugin that takes a directory as input and process all images in that directory by itself. Called from the Gimp UI, but doesn't require user interaction once started.
Reply
#5
(02-12-2023, 12:39 AM)Ofnuts Wrote: You don't need to open the UI(*), you can use Gimp+Python in batch mode from a shell (or shell script), but your script still has to start a Gimp process and tell it to run your Python. See this example.

(*) Although another option is to write a plugin that takes a directory as input and process all images in that directory by itself. Called from the Gimp UI, but doesn't require user interaction once started.

Tried several things but I'm getting errors:
I'm on GIMP 2.10.28

GIMP-Warning: The batch interpreter 'python-fu-eval' is not available. Batch mode disabled.

or when modifying the command:

GIMP-Warning: The batch interpreter 'python-fu-eval-image' is not available. Batch mode disabled.

My shell command is:
gimp -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['/home/Kaitlyn/Desktop']+sys.path;import ChangeGimpTextLayersSample;ChangeGimpTextLayersSample.run('/home/User/Desktop/FieldSheet.xcf')" -b "pdb.gimp_quit(1)"

The script, called ChangeGimpTextLayersSample.py is in the same folder as the XCF fil (i.e right on the Desktop)
And the contents of the script is below: 


Code:
import sys
import os
import traceback
import logging
from gimpfu import *
import gimpcolor
from collections import namedtuple

logging.basicConfig(filename='/home/User/Desktop/gimp_script_log.log', level=logging.DEBUG)

file_path = "/home/User/Desktop/FieldSheet.xcf"

if os.path.exists(file_path):
    logging.debug("Found FieldSheet.xcf")
    image = pdb.gimp_xcf_load(0, file_path, file_path)
    layers = image.layers
    for layer in layers:
        if layer.name == "ChangeMe":
            logging.debug("Found ChangeMe layer")
            pdb.gimp_text_layer_set_text(layer, "Testing Script")
    pdb.gimp_xcf_save(0, image, image.active_layer, "/home/User/Desktop/test.xcf", "test.xcf")
    logging.debug("Finished processing FieldSheet.xcf")
else:
    logging.debug("The file does not exist.")
Reply
#6
See my remark above about the availability of the Python support in Gimp in recent Linux distros.

You will know you have a working Python support when you have a working Filters > Python-fu > Console in the UI.
Reply
#7
(02-12-2023, 09:46 AM)Ofnuts Wrote: See my remark above about the availability of the Python support in Gimp in recent Linux distros.

You will know you have a working Python support when you have a working Filters > Python-fu > Console in the UI.

FWIW
[Image: gimp-console.png]

(02-12-2023, 12:39 AM)Ofnuts Wrote: You don't need to open the UI(*), you can use Gimp+Python in batch mode from a shell (or shell script), but your script still has to start a Gimp process and tell it to run your Python. See this example.

This is what I tried to do here:

Code:
gimp -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['/home/Kaitlyn/Desktop']+sys.path;import ChangeGimpTextLayersSample;ChangeGimpTextLayersSample.run('/home/User/Desktop/FieldSheet.xcf')" -b "pdb.gimp_quit(1)"
Trying to call this:
Code:
import sys
import os
import traceback
import logging
from gimpfu import *
import gimpcolor
from collections import namedtuple

logging.basicConfig(filename='/home/User/Desktop/gimp_script_log.log', level=logging.DEBUG)

file_path = "/home/User/Desktop/FieldSheet.xcf"

if os.path.exists(file_path):
    logging.debug("Found FieldSheet.xcf")
    image = pdb.gimp_xcf_load(0, file_path, file_path)
    layers = image.layers
    for layer in layers:
        if layer.name == "ChangeMe":
            logging.debug("Found ChangeMe layer")
            pdb.gimp_text_layer_set_text(layer, "Testing Script")
    pdb.gimp_xcf_save(0, image, image.active_layer, "/home/User/Desktop/test.xcf", "test.xcf")
    logging.debug("Finished processing FieldSheet.xcf")
else:
    logging.debug("The file does not exist.")
Reply
#8
What you show is the script-fu console, not the python-fu one.

   
Reply
#9
(02-12-2023, 02:11 PM)Ofnuts Wrote: What you show is the script-fu console, not the python-fu one.

Thanks for further clarifying. I was able to scour:
• gimp-python_2.10.8-2_amd64.deb
• python-gobject-2_2.28.6-13+b1_amd64.deb
  and
• python-gtk2_2.24.0-6_amd64.deb.

But python-cairo is not anywhere to be found for bullseye, searches land on python3-cairo_1.16.2-4+b2_amd64.deb or python-cairo for Trusty and really old versions of Ubuntu/debian/whatever.

When I installed the packages above dpkg pukes saying that it's leaving them unconfigured because python 2.7 is not installed (but it is actually installed!!!).

If you don't mind, and in all frankness, an ambulance driver can save more lives if he doesn't have to also become a car mechanic. Sure, an ambulance driver shouldn't have to limit himself and with enough determination he could become an astronaut but for all intents and purposes, he'll be more valuable to society if we give him the fish so he could have a quick lunch and so that he could keep working as an ambulance driver rescuing people rather than if we gave him a fishing rod. Can I please give you back the fishing rod and beg you for a download link that leads to an actual file and could you please list a series of easy steps that I can blindly follow to get this thing resolved?
Code:
$ uname -a
Linux e6420MX 5.19.0-4.2-liquorix-amd64 #1 ZEN SMP PREEMPT_DYNAMIC liquorix 5.19-5~mx21+1 (2022-08-30) x86_64 GNU/Linux

My scope of  expertise comes from a completely different and unrelated field, I'm in my mid 50's and my only shot at coding is ChatGPT because it codes for me and I do what I can to keep it on track as it has plenty of limitations. It often "forgets" what steps we have already tried and goes in circles. My little background in programming is Visual Basic 5 and it was at hobby level at best. Not trying to make excuses but otherwise, other than being a regular Linux user, I do not even understand well how the entire inter dependency of packages work.

I can still run and install 32 bit apps compiled in Windows 98 in Windows 11 of all OSs but I am in disbelief that in Debian, some bright minds decided to deprecate and hide these older packages in the new distros even though the new versions of these packages aren't a fit replacement for the old ones. Burying by that the efforts of so many plugin developers in a single update. WTF?

Going back to a possible solution: I have already installed python 2.7. Is there an Appimage that might have python-gimp in it? I don't care about the healing plugin or whatever that is. I'd like to avoid to switch to Buster or older just to get this resolved.

At this stage in life I am in a quest to close cycles already open rather than opening an infinite set of new learning threads for "the sake of learning". I work everyday of the week to keep a roof over my head yet wasted my entire weekend "learning" deciphering this gimp-python thing. I'm grateful for the tire that's being thrown at me while drowning in the water, those efforts to save my life would succeed under the assumption that I can swim though. Any chance someone can come on a boat before I drown instead? I mean, most of what I'm reading here is long and above my immediate scope of understanding. Can we please put aside the "teach a man to fish and you fed him for the rest of his life" approach and give this ambulance driver some fish instead so he can quickly focus his efforts in his rescue operation?
Reply
#10
(02-13-2023, 05:01 PM)Anthony Buff Wrote: Going back to a possible solution: I have already installed python 2.7. Is there an Appimage that might have python-gimp in it? I don't care about the healing plugin or whatever that is. I'd like to avoid to switch to Buster or older just to get this resolved.

I believe current AppImages already have a built-in Python interpreter. So does the flatpak version. Not an expert in either because I went the 3rd route which is to compile my own version. Maybe see this or ask in the Linux forum.
Reply


Forum Jump: