2

I have a set of images that I am trying to process manually, so I wrote a simple plug-in to try and help make it a little less painful. The images are numbered sequentially. So when I finish working on one image I can hit a key combo and it saves the file with the correct name and opens the next one in the list. This all works fine.

The issue I have with the plug-in is that for newly opened images the undo-redo history does not work. I am not sure if I am not loading it in correctly, or if there is something that I have to set for the undo history to be enabled. When the new image is loaded the undo history is completely blank, and when I make changes to the image nothing new is added to it as I work on the file. This makes it difficult to do anything as a mistake means starting over. I am using the latest gimp version 2.8.18.

I am a complete newb at writing gimp plug-ins and built this from web examples, so any help to figure out why this is happening would greatly appreciated. Here is the code I wrote for the plug-in:

#!/usr/bin/python

from gimpfu import *
import os.path

def get_next_filename(filepath):
    #Now figure out the next image to load
    old_file = filepath.replace("\\", "/")
    file_info = old_file.split('/')
    #print file_info

    name = file_info[len(file_info) - 1].replace(".JPG", "").replace("P", "")
    print name

    next_num = int(name) + 1

    new_name = "P" + str(next_num) + ".JPG"
    print new_name

    file_info[len(file_info) - 1] = new_name
    s = "/"
    new_path = s.join(file_info)
    print new_path
    return new_path

def plugin_main(timg, tdrawable):
    print "orig filename: ", timg.filename
    new_filename = timg.filename.replace(".JPG", "_colored.JPG")
    print "new filename: ", new_filename
    layer = pdb.gimp_image_merge_visible_layers(timg, CLIP_TO_IMAGE)

    if not os.path.isfile(new_filename):
        pdb.gimp_file_save(timg, layer, new_filename, '?')
    else:
        pdb.gimp_message ("Failed to save segmented image. File " + new_filename + " already exists.")

    print "Closing image"
    #pdb.gimp_display_delete(gimp._id2display(0))

    #Try to load one of the next 5 consecutive image numbers.
    #If none found then quit plugin, else load it.
    new_path = timg.filename
    for i in range(0,5): 
        new_path = get_next_filename(new_path)

        if os.path.isfile(new_path):
            print new_path + " found. Loading it."
            img = pdb.file_jpeg_load(new_path, new_path)
            pdb.gimp_display_new(img)
            return  

    pdb.gimp_message ("No more consecutive images were found in the folder.")



register(
        "segmented_saver",
        "Saves a segmented image with the correct name and opens next image.",
        "Saves a segmented image with the correct name and opens next image.",
        "DC",
        "DC",
        "2016",
        "<Image>/Image/Save Segmented...",
        "RGB*, GRAY*",
        [],
        [],
        plugin_main)

main()
dcofer
  • 303
  • 2
  • 10
  • 1
    [This thread](http://gimpchat.com/viewtopic.php?f=8&t=8617) suggests that plugins *can* break the undo history, but mostly if they tackle the history themselves. Anyway, don't you mean yout `if os.path.isfile(new_path)` block to be inside the 5-element `for` loop? – Andras Deak -- Слава Україні Oct 12 '16 at 21:48
  • 2
    I don't even understand how this runs because your registration code isn't declaring any parameter so plugin_main must be receiving "None" for both parameters. I have a very similar plugin ([ofn-file-next](http://sourceforge.net/projects/gimp-path-tools/files/scripts/)) and no such issues with it... – xenoid Oct 12 '16 at 23:46
  • You are correct. When I was formatting this for the post I got the indents for that if block wrong. – dcofer Oct 13 '16 at 15:29
  • I do not see an ofn-file-next.py plugin at the link you provided? – dcofer Oct 13 '16 at 15:35

1 Answers1

1

Just call image.enable_undo(). For example:

img = pdb.gimp_file_load('R:/1.jpg','1.jpg')
gimp.Display(img)
img.enable_undo()
csh
  • 21
  • 1