Search code examples
pythongimppython-fu

Gimp python-fu : How to create and save multilayer xcf from 1 jpg and 1 png?


I've a huge list of directories containing for each 2 images : 1 jpg file and 1 png file; I want to create from an external script all the .xcf associated with the jpg as first layer. After trying with python I've been drowning in scheme explanations and other linked subjects ... Can someone help me to make such command and understand it ?


Solution

  • Typically, something like this, for each pair of images:

    image=pdb.gimp_file_load(jpgImage,jpgImage)
    layer=pdb.gimp_file_load_layer(image,pngImage)
    image.add_layer(layer,0)
    pdb.gimp_xcf_save(0,image,layer,outputImage,outputImage)
    

    The ofn-coalesce-images script does something very similar (stacks all images found in a directory)...

    To run such a thing in headless Gimp, consider a .py that will run staring from some specified root directory:

    def run(directory):
        # Iterates subdirs and creates images
    

    This .py doesn't need to be formally a python plugin (no need to register() anything). Then you can launch it using:

    Unix-ish:

    gimp -idf --batch-interpreter python-fu-eval -b 'import sys; sys.path=["."]+sys.path;import batch;batch.run("/some/path")' -b 'pdb.gimp_quit(1)'
    

    Windows:

    gimp -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('/some/path')" -b "pdb.gimp_quit(1)"
    

    (this is a bit convoluted because Linux and Windows have somewhat opposite ways to deal with quotes and double quotes in the command line).