GIMP: Create image stack from all image files in folder

后端 未结 2 1647
梦毁少年i
梦毁少年i 2021-01-14 09:53

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

相关标签:
2条回答
  • 2021-01-14 10:19

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

    0 讨论(0)
  • 2021-01-14 10:38

    "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.

    0 讨论(0)
提交回复
热议问题