2

手動で処理しようとしている一連の画像があるので、簡単なプラグインを作成して、少し痛みを軽減できるようにします。画像には順番に番号が付けられています。そのため、1 つの画像の作業が終了したら、キー コンボを押すと、正しい名前でファイルが保存され、リスト内の次のファイルが開きます。これはすべてうまくいきます。

プラグインで私が抱えている問題は、新しく開いた画像の取り消しとやり直しの履歴が機能しないことです。正しくロードしていないのか、元に戻す履歴を有効にするために設定する必要があるものがあるのか​​ わかりません。新しい画像が読み込まれると、元に戻す履歴は完全に空白になり、画像に変更を加えても、ファイルで作業しても新しいものは何も追加されません。これは、間違いが最初からやり直すことを意味するため、何かをすることを困難にします。最新の gimp バージョン 2.8.18 を使用しています。

私は gimp プラグインを作成するのがまったくの初心者であり、これを Web の例から作成したので、なぜこれが起こっているのかを理解するための助けをいただければ幸いです。プラグイン用に書いたコードは次のとおりです。

#!/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()
4

1 に答える 1