Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python-Fu - pdb.query ; how to filter to readable output
#1
Caveat: I'm new to Python & Object-Oriented Programming (and not particularly skilled with GIMP)

I'm trying to learn a little about automating GIMP, and found this tutorial:

https://www.gimp.org/tutorials/Automate_Editing_in_GIMP

I was able to do the first little example, finding the python console within GIMP, and learned quite a bit, enough to do some experimenting on my own.

One thing I wanted to experiment with was setting the grid color, and I guessed at several properties ("attributes"?) of the object (?) "pdb" (which I googled to discover was GIMP's "procedural database", which I take to mean the database of scriptable procedures within GIMP), but none of my attempts (like "pdb.gimp_image_grid_set_fgcolor()" or pdb.gimp_image_grid_set_foreground_color()", etc) worked. So I figured there must be a way to list all the attributes ("properties"?) of an object, and after quite a bit of googling, came up with two [relatively easy, one-liner) possibilities:

dir(pdb)

and

pdb.query()

(I see they produce different info, so I'm unclear as to which, if either, will be my better option, but that's a question for later).

The problem is that both commands give too much information to look through efficiently.

So I did yet more googling, and came up with:

re.findall("grid",str(pdb.query()))

hoping to just get the items mentioning "grid", but all that returned was a bunch of "grid"s.

In bash, I'd try something like "pdb.query() | grep grid" (which might or might not give me usable results).

So, my questions:

1. Which is better and why, pdb.query() or dir(pdb), or are neither of these suitable for learning about the attributes/properties of an object?

2. Is there a better way of learning about the properties/attributes of an object (to figure out what attribute/property to tinker with to change the color of the grid a la the tutorial mentioned above?

3. How do I list all the attributes/properties of an object that has a lot of these attributes/properties and filter the results or present them in a way that is digestible, particularly as it relates to the output of "pdb.query" or "dir(pdb)"?

Thanks!

--
Kent
Reply
#2
UPDATE:

I thought to try the more complete string:

re.findall("gimp_image_grid_set",str(dir(pdb)))

and that returned five hits. The tutorial gave me three of them, so I just did a brute-force search, going through the alphabet::

re.findall("gimp_image_grid_set_a",str(dir(pdb)))

resulted in nothing.

re.findall("gimp_image_grid_set_b",str(dir(pdb)))

gave me a hit. So I moved on to the next charavter, starting the alphabet over again at "a":

re.findall("gimp_image_grid_set_ba",str(dir(pdb)))

Getting another successful hit, I guessed this was going to be "background", so I tried

re.findall("gimp_image_grid_set_back",str(dir(pdb)))

worked, and

re.findall("gimp_image_grid_set_background",str(dir(pdb)))

worked. Continuing like this, I found the two missing attributes/properties are:

gimp_image_grid_set_background_color()

and

gimp_image_grid_set_foreground_color()

With further experimenting (googling didn't reveal the syntax), I found I can set the color with either:

pdb.gimp_image_grid_set_foreground_color(theImage, "red")

or

pdb.gimp_image_grid_set_foreground_color(theImage, "#FF0000")

But brute-force searching is a dirty hack; I need to know how to properly find the attributes/properties of an abject, and how to use them.

Thanks!

--
Kent
Reply
#3
In Gimp's Python console, it the "Browse" button,this opens another dialog with a left pane where you can list/search procedures, and a right pane that documents the procedure parameters. The "Apply" biutton inserts a sample called in the console window.

Stop everything, and find a tutorial on Python'c "comprehensions". Comprehensions are very nice way to create lists arrays, and can be used to filter existing lists/arrays, for instance:

Code:
[p for p in dir(pdb) if 'image' in p]

PS: The tutorial you mention is awful IMHO.
Reply
#4
Ah, great! That "Browse" button is great.

I googled (well, duckduckgo'd) "Python's comprehension", and took a look at https://python-3-patterns-idioms-test.re...sions.html.

Whereas I can kind of follow what's being said in that tutorial, it's heavy slogging (i.e, <i>work</i>); I think I'll just make a mental not for now to absorb "comprehensions" as I mature in the use of Python, and just continue <i>play</i>ing for now so I don't lose interest. After recovering a bit from the brain-hurt of that site I found, I may muster up the courage to search for an easier-to-comprehend explanation.

Your code example is great, too! I don't completely comprehend what it's doing, but I get the gist, and can probably adapt it as needed in the future, to some extent.

I've found that a lot of tutorials are awful; this was one that at least got me started. But it wasn't long before I realized I wasn't really getting out of it what I needed. Still, it got my feet wet.

And finally, how does one go about suggesting corrections to the information presented by the "Browse" button. In this case, for the two "set color" commands:

1) the info window says the commands "get" the color rather than "set" the color,

2) the command is shown as hyphenated rather than underscored,

3) there really should be an example of how the command is called (the syntax)

If a Joe User such as myself could help to make GIMP/Python/Linux better make by making those corrections (or filing a bug against the documentation, etc), I'd like to help contribute to making things better.

Thanks for your reply! You've made things better, and I appreciate it!

--
Kent
Reply
#5
1) which call(s) exactly? For bugs reports see https://gitlab.gnome.org/GNOME/gimp/issues
2) Pervasive problem. The command names are the names used in Script-fu. All this help is generated automatically from the
registration information.
3) hit the Apply button in the help, and see what you get in the Python console window.
Reply
#6
(10-07-2018, 10:02 PM)Ofnuts Wrote: 1) which call(s) exactly? For bugs reports see https://gitlab.gnome.org/GNOME/gimp/issues


Code:
gimp-image-grid-set-background-color

and


Code:
gimp-image-grid-set-foreground-color


2) Pervasive problem. The command names are the names used in Script-fu. All this help is generated automatically from the registration information.

Since posting that, I've discovered it's pervasive, as if by design. If I understand you, the commands were originally hyphenated in Script-Fu, and when "copied" to PythonFu, they were converted to underscores, without converting the corresponding documentation. It helps me to understand that.


3) hit the Apply button in the help, and see what you get in the Python console window.

That helped, but not a lot. The result was:


Code:
pdb.gimp_image_grid_set_foreground_color(image, fgcolor)


What I believe would have been better is something like:


Code:
EXAMPLES:

After having set a variable (such as "theImage") with the name of the image to modify (such as with "theImage = gimp.image_list()[0]"):

pdb.gimp_image_grid_set_foreground_color(theImage, "#FF0000")

or

pdb.gimp_image_grid_set_foreground_color(theImage, "red")

Of course, others might disagree.

--
Kent
Reply


Forum Jump: