簡體   English   中英

Gimp Python 插件加載圖像禁用撤消歷史

[英]Gimp Python plugin loading image disables undo history

我有一組要手動處理的圖像,因此我編寫了一個簡單的插件來嘗試幫助減輕痛苦。 圖像按順序編號。 因此,當我完成一個圖像的工作時,我可以點擊一個組合鍵,它會用正確的名稱保存文件並打開列表中的下一個。 這一切正常。

我對插件的問題是,對於新打開的圖像,撤消重做歷史不起作用。 我不確定我是否沒有正確加載它,或者我是否必須設置一些東西才能啟用撤消歷史。 加載新圖像時,撤消歷史記錄完全為空白,當我對圖像進行更改時,在處理文件時不會向其中添加任何新內容。 這使得做任何事情都變得困難,因為錯誤意味着重新開始。 我使用的是最新的 gimp 版本 2.8.18。

我是編寫 gimp 插件的完全新手,並從網絡示例中構建了它,因此任何幫助弄清楚為什么會發生這種情況的人都將不勝感激。 這是我為插件編寫的代碼:

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

只需調用image.enable_undo() 例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM