GIMP: Create image stack from all image files in f

2019-02-25 18:09发布

问题:

I need to compare the results of segmentation algorithms where lots of images need to be stacked - e.g. original and binary image. So I thought of a GIMP script which takes the name of a directory and puts all containing image files into layers so that they can be switched on and off in GIMP to compare results. How to achieve this with GIMP? Thank you for your tips!

Regards

回答1:

"Not with script-fu". But Python is suitable to your needs. It is a simple script - the core logic should be 4 lines or so, therefore I will just write it here for you:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from gimpfu import *
import os

def load_images_in_dir(image, drw, path):

    for filename in os.listdir(path):
        try:
            if filename.lower().split(".")[-1] in ("png", "jpg"):
                #import pdb as debug; debug.set_trace()
                image_id, layer_ids = pdb.gimp_file_load_layers(image,
                     os.path.join(path, filename))
                for id in layer_ids:
                    new_layer = gimp.Item.from_id(id)
                    pdb.gimp_image_add_layer(image, new_layer, 0)
        except Exception, error:
            print error



register(
        "open_images_in_dir",
        "Open all files in a directory",
        "Open all files in a directory",
        "Joao S. O. Bueno",
        "Joao S. O. Bueno",
        "2012. Creative Commons Citation Needed license",
        "Open Images in Dir as Layers...",
        "*",
        [(PF_IMAGE, "image", "the image", None),
         (PF_DRAWABLE, "drw", "the drawable", None),
         (PF_DIRNAME,"path", "Directory to Open", "."),],
        [],
        load_images_in_dir,
        menu="<Image>/File/")

main()

Note that the second part of the code is just the boilerplate for registering a function. Indeed - the call "gimp_file_load_layers" does not work as it should - as it returns a list of object "id"s which are intended not to be seem from Python - but the call to "Item.from_id" method allows one to bypass this inconvenience. This is only available in gimp-2.8, though

To get this to work in gimp 2.6 you will have to resort to open the file ina new image, and them copy the layer(s) to your target image.

Copy the script above to a GIMP's plug-ins directory (under *nix, ~/.gimp-2.8/plug-ins, for example - or check edit->prerencers->folders within GIMP for a plug-in folder) - and mark it as executable.



回答2:

I just realized that the command "file" > "open as layers" in gimp 2.8 does a similar job!