Charlweed
Charlweed

Reputation: 1614

Gimp python-fu , open a directory selection dialog

I want the user of a script to be able to select a directory to do "stuff". Not open a JPG or save a XCF. I just need a directory, because my script will do stuff including reading multiple files, and save multiple transformations.

I am supposed to be able to use

import Tkinter, tkFileDialog
root = Tkinter.Tk()

But gimp 2.10.24 has a... custom... install of the end-of-life python 2.7, and I get the error This probably means that Tcl wasn't installed properly.

So is there something like "pdb.open_directory" built into gimp? What about other python libraries shipped with gimp, but actually work? I'd also be satisfied if I could get Tkinter working in gimp, but that seems even harder.

Upvotes: 0

Views: 898

Answers (1)

xenoid
xenoid

Reputation: 8914

If you use the auto-generated parameter dialog for your Python-fu, then it's just a matter of using a parameter with a PF_DIRNAME type. For instance

register(
    'export-tiles',
    "Export tiles","Export tiles",author,author,year,"Export tiles...",
    '*',
    [
        (PF_IMAGE,      'image',        'Input image', None),
        (PF_DIRNAME,    'directory',    'Directory',   '.'),
        (PF_STRING,     'namePattern',  'Tile name',   '{imageName}-{column1:02d}-{row1:02d}.png'),
        (PF_SPINNER,    'rows',         'Rows',        10, (1,1000,1)),
        (PF_SPINNER,    'columns',      'Columns',     10, (1,1000,1))
    ],
    [],
    exportTiles,
    menu="<Image>/File/"
)

will elicit a dialog like this:

enter image description here

where the Directory button opens a directory selector and the function defined as:

def exportTiles(image,directory,namePattern,rows,columns):

will automatically receive a gimp.Image object in image (the active image when you call the script), a directory as a python string ('/tmp') in directory, the name pattern (as a python string) in namePattern, and rows and columns as python floats.

Upvotes: 1

Related Questions