Post Reply 

Get current image with script-fu?

Jun 11, 2014, 02:18
Post: #1
Get current image with script-fu?
Hi there, I'm a long time GIMP user and I decided to start making a script to help me perform some repetitive functions for me. I'm no stranger to programming and I'm pretty sure I understand how script-fu generally works.

However, I can't find out how to get the current/active image's ID. That is, the one the user is currently viewing. I'd imagine this would be fairly straightforward, can anybody point me in the right direction?
Find all posts by this user
Quote this message in a reply
Jun 11, 2014, 06:34
Post: #2
RE: Get current image with script-fu?
If you have a look at the basic Scheme tutorial on the GIMP web-site, it shows how you write a script that is passed the image (and drawable) id from the script-dialog window.

http://www.gimp.org/tutorials/Basic_Scheme/

Kevin
Find all posts by this user
Quote this message in a reply
Jun 11, 2014, 09:20
Post: #3
RE: Get current image with script-fu?
(Jun 11, 2014 02:18)puggsoy Wrote:  Hi there, I'm a long time GIMP user and I decided to start making a script to help me perform some repetitive functions for me. I'm no stranger to programming and I'm pretty sure I understand how script-fu generally works.

However, I can't find out how to get the current/active image's ID. That is, the one the user is currently viewing. I'd imagine this would be fairly straightforward, can anybody point me in the right direction?

If the first parameter to your script is an image, then it is automatically set to the image the user is currently viewing. Likewise if the second parameter is a "drawable" (layer or channel), then it is set to the current/active layer/channel for the image. As a consequence, the dialog generated to requests the input parameters will not have input fields for these two.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Jun 11, 2014, 22:39
Post: #4
RE: Get current image with script-fu?
Thanks guys! After I posted I actually took a second look at the Basic Scheme tutorial as paynekj suggested and found my answer, apparently I managed to miss it the first time round. I didn't have time to post that I'd figured it out though.

I do have another question though, of a different nature, and I'm not sure if I should make a new thread (please say if I should) but here it is anyway:

Is there a way to make a script-fu tool that I can simply use by selecting the tool and clicking on the image, which then passes through the cursor's coordinates? Similar to any of the built-in tools, but then it uses the coordinates to do my custom function.

If this isn't possible in script-fu, can I do it with Python?
Find all posts by this user
Quote this message in a reply
Jun 11, 2014, 23:43 (This post was last modified: Jun 11, 2014 23:51 by ofnuts.)
Post: #5
RE: Get current image with script-fu?
Not even sure you can do it with python(*)... that's the difference between tools and the plugins. When you need actual coordinates in a script, the usual technique is to request a path, and obtain the points from the path. You can also ask for a pair of guides (but things get difficult if the user has other guides).

The attached (python) script uses a path (with multiples strokes) to be told where to copy a layer (with perspective).

(*) On the whole python can still do a lot more things than script-fu, and is a lot easier to code for (and it has a lot more uses outside of Gimp).

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Jun 13, 2014, 07:52
Post: #6
RE: Get current image with script-fu?
Well I've decided to just use the top-left corner of a rectangular selection for the coordinates, that shouldn't really be an issue since it's for a specific purpose so only me and perhaps a few other people will use it.

Is Python also quicker with processing long tasks? For instance would it be faster to go through and check every pixel in an image? (Of course I don't plan to do exactly that but my plugin will go through some fairly long loops.) If so I might consider using that then, I picked up script-fu in less than 2 days so if Python is easier it shouldn't take too long either.
Find all posts by this user
Quote this message in a reply
Jun 13, 2014, 10:34
Post: #7
RE: Get current image with script-fu?
Usually you try to avoid that, but yes, python has a faster access to the pixels.

If you can code in any language, Python is yours in one hour. I introduced one colleague to it and he had his first industrial-production-grade script working in a day.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Jun 13, 2014, 23:56
Post: #8
RE: Get current image with script-fu?
Well I've got Python figured out and you're right, it is much easier than script-fu. I am having some trouble though, if I try to do anything like for loop or conditional (if) statement, it says the "plugin crashed" when I try to run it. For example, this runs:

Code:
def iconMaker(img, drawable):
    bounds = pdb.gimp_selection_bounds(img)
    
    print bounds[0]

But this doesn't:

Code:
def iconMaker(img, drawable):
    bounds = pdb.gimp_selection_bounds(img)
    
    if true
        print bounds[0]

Am I doing something wrong?
Find all posts by this user
Quote this message in a reply
Jun 14, 2014, 00:37
Post: #9
RE: Get current image with script-fu?
1) check your syntax:
Code:
if true
Should be:
Code:
if true:

(with a colon).

In python unlike most other languages, there are no block markers (begin/end, braces) but indenting is significant. And indenting with tabs isn't the same as indenting with spaces (you can use either, but you should stick to it).

You can check the more blatant syntax error by just running the script outside of Gimp:
Code:
python thescript.py
If it complains about missing gimpfu, then the syntax is OK, and you can try to run it in Gimp.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Jun 14, 2014, 01:29
Post: #10
RE: Get current image with script-fu?
Ah right, I must have missed the colon when looking up references! Works great now, thanks.

Also thanks for that tip, I was wondering if there's a good way to debug Python scripts. Hopefully I should be able to start properly working on it now!
Find all posts by this user
Quote this message in a reply
Jun 14, 2014, 08:41
Post: #11
RE: Get current image with script-fu?
As Brian Kernighan wrote, "The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.".

The main problem with Gimp script is that on Windows, the output of plain print statements cannot be seen, so you have to use gimp_message() instead, and start the Error console dialog (otherwise you get one dialog for each gimp_message(). On Linux (and maybe OSX) things are much easier.

There is a version of the Python interpreter with a built-in debugger but I don't know if anyone tried to make it work with Gimp.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Jun 14, 2014, 23:36
Post: #12
RE: Get current image with script-fu?
Yeah, printing is invaluable, many a time has it allowed me to pinpoint which part of complex code is causing an error-less crash (or on which iteration of a loop it's happening). I realised print isn't showing anything so I've been using gimp_message(), but I didn't know about the Error console, thanks! I've also decided to wrap gimp_message() in my own trace() function, to make it a bit quicker to write.
Find all posts by this user
Quote this message in a reply
Jun 15, 2014, 01:17 (This post was last modified: Jun 15, 2014 01:18 by ofnuts.)
Post: #13
RE: Get current image with script-fu?
If your code isn't too big you can also run and debug it from the python-fu console.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Jun 15, 2014, 01:26
Post: #14
RE: Get current image with script-fu?
Does the python-fu console display print/gimp_message()? As far as I could tell it only displays return values.
Find all posts by this user
Quote this message in a reply
Jun 15, 2014, 14:12
Post: #15
RE: Get current image with script-fu?
I tried a few things, and with a bit of twiddling you can run your plug-in in the Gimp python-fu console, and see the output of print statements. Just made a short tutorial about this, see there:

http://gimpforums.com/thread-debugging-y...fu-console

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Scale image to specific size MattRothschild 1 173 Sep 15, 2017 11:13
Last Post: ythgilb
  Request for script chlebna 1 130 Sep 13, 2017 09:35
Last Post: ythgilb
  Request for script - Guillotine + Watermark sunslick 1 195 Sep 4, 2017 08:51
Last Post: ythgilb
  Script-fu: How to create new, addressable layer using gimp-floating-sel-to-layer Bertholomäus 2 182 Aug 31, 2017 14:32
Last Post: Bertholomäus
  How to modify this script-fu? Zakary81 1 152 Aug 23, 2017 12:48
Last Post: paynekj

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS