Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Choosing to learn Python-Fu or Script-Fu for a simple script
#1
Hello, I want to use GIMP to extract 200 small images from a set of larger images, which are uniformly structured. I have figured out how to use image_select_rectangle to do the initial selection, in both Python fu and Script fu, though in Python I am still working on learning the syntax to identify [Image] correctly.

I need to flush out a script that will copy the selection to a new file and save it to a folder, with an incremented filename each time.

I will also need to write a bit of code to update the x,y coordinates of the corner of each selection, which are 4x5 groups of 5x2 images (200 total images). I have basic programming skills but I haven't really used them in several decades - I learned C++ when it was - BRAND NEW.

So I am wondering which of the two scripting modules will be easier to use to write a small/short routine to increment the x,y value for each selection from the larger image? 

A hint towards the basic increment variable routine that would be used to increment the parameters in image_select_rectangle would certainly speed things up. As would the needed syntax on incrementing the file name on the output call. Any tips would be much appreciated.
Reply
#2
Well still working on deciding which to delve deeper in to.

I figure a good first step is to get all the function calls working manually in the console window.

With Python, I am still struggling just to correctly pass the parameter identifying the image I want to copy from. If I try to define "Image" before calling select-rectangle, I can't get the Python console to load a file. It wants the full path and the file name, and also the "file name as entered by user", whatever that is - something different than the full path? All combinations I try just return a 'Syntax error' in the console.

Alternately, I can try to identify the active image as part of the call to select-rectangle, but I am having no luck setting that parameter correctly either. I know with Python-fu identifying the object is not a single integer as it is in Script-fu. But I can't get the syntax right in this call either.


Meanwhile, since all of the Procedures in Gimp were written for Script-fu first anyway, it appears, using Script should have some advantages - including a deeper bench of tutorials scattered around the 'net. For such a small operation of just moving a pointer around an image, I will be able to handle the essentially RPN math notation that Scheme wants to use. And I have discovered the Loop syntax to use in Scheme and thus in Script-fu, so incrementing the pointer location shouldn't be too hard, except....

I can't get the Script-fu Copy Selection & then Paste Selection into a new file. The options on copying a selection (I have that working correctly) copy to a new channel, or to a buffer, or to a layer, and then those are probably floating, and I probably have to anchor whichever it is, either before I Paste, or after, and I can't get the most seemingly easy-peasy Copy Selection & Paste Selection even working manually in the console yet either.
Reply
#3
Your script looks a lot like my ofn-tiles script  (not to be confused with ofn-layer-tiles :Smile that you can at least use as an example.

I asked myself the same question 8 years ago, and I went with Python, and I'm glad I did. Python is a lot easier to learn (and closer to C/C++). It also has a lot more uses outside of Gimp. Finding help with Python itself is easy.

In Gimp, the Python API is a superset of the script-fu one (all the pdb.* function are direct mapping of the script-fu functions). In additiion:
  • in Python you deal with objects, and these objects have attributes and methods that will replace the plain API and make your code more readable:
Code:
if layer.visible
# vs
if pdb.gimp_item_get_visible(layer):
  • there are additional functions to efficiently deal with pixels (pixels are converted to Python arrays)
Python syntax has its quirks. In particular,  you won't see open/close braces for blocks, everything is determined by the indentation of the code (which must be coherent, either spaces, or tabs, but don't mix lines with tabs with lines with spaces).  This means that even though it is doable when you know, writing a loop in the console isn't very easy.

To load your image, just do:
Code:
image = pdb.gimp_file_load('/tmp/sample.png','/tmp/sample.png')

The second parameter is what is used in error messages, it can sometimes be different from the "technical" file name (resolve links, case, added default extension...).

If you are in the Python-fu console, you can obtain the currently open image using:
Code:
image = gimp.Image_list()[0]

(of course if you have more than one image  gimp.Image_list() is an array with several elements. To locate your image you can just print it:
Code:
images = gimp.Image_list()
print images
image=images[2] # Pick your image

One big advantage of Python is that script-fu is limited to a tiny core and there is very little you can do outside of the Gimp API. WIth Python you can use a much broader runtime (function to parse strings, manage files, etc...) and you can also use (after installing them) modules from the very extensive Python libraries.

To make a loop with x,y, stepping the X by 20 and the Y by 10:
Code:
for x in range(0,200,20):
   for y in range(0,200,10):
       print x,y

For complicated loops, in Python can you can also use a "generator":
Code:
# The generator
def iterateXY(sizeX,sizeY):
   for x in range(0,200,sizeX):
       for y in range(0,200,sizeY):
           yield x,y

# Using it.
# This is a one-level loop, even though it iterates all the x,y pairs.
for x,y in iterateXY(40,80):
   print "%3d, %3d" % (x,y)

The big advantage of the generator technique being that in your main code you have no nested loops, so you can easily break out of the whole thing. Another thing you will like is the for/else construct, where the else clause is executed only oif the loop runs to its end (no break).

For hints to debug your Python code in Windows, see https://www.gimp-forum.net/Thread-Debugg...in-Windows.
Reply


Forum Jump: