Gimp3のPython-fuでPSD形式でエクスポートするプラグイン

玉洲習画帖 第5巻 コンピュータ
出典:国立国会図書館「NDLイメージバンク」(https://ndlsearch.ndl.go.jp/imagebank)

xcfと同名でpsd形式で画像をエクスポートします。
エクスポートする機能は標準で存在するので、わざわざ作る必要が無いプラグインではありますが、

ファイルパス:my-export-psd\my-export-psd.py

#!/usr/bin/env python3
# XCFのパスにPSDをエクスポートするスクリプト
import sys, gi, subprocess
import datetime, os, time
gi.require_version('Gimp', '3.0')
from gi.repository import Gimp, GObject, Gio, Gegl
PROC = "python-fu-my-export-psd"
def run(proc, run_mode, image, drawables, config, data):
    if not image:
        return proc.new_return_values(Gimp.PDBStatusType.CANCEL, None)
    file_obj = image.get_file()
    if not file_obj:
        return proc.new_return_values(Gimp.PDBStatusType.CANCEL, None)
    xcf_path = file_obj.get_path()
    base, _ = os.path.splitext(xcf_path)
    psd_path = base + ".psd"
    out_file = Gio.File.new_for_path(psd_path)
    result = Gimp.file_save(Gimp.RunMode.NONINTERACTIVE, image, out_file, None)
    if result:
        Gimp.message(f"エクスポート完了: {psd_path}")
    else:
        Gimp.message("エクスポートに失敗しました。")
    Gimp.displays_flush()
    return proc.new_return_values(Gimp.PDBStatusType.SUCCESS, None)
class RunExportPSD(Gimp.PlugIn):
    def do_query_procedures(self):
        return [PROC]
    def do_create_procedure(self, name):
        if name != PROC:
            return None
        p = Gimp.ImageProcedure.new(self, name, Gimp.PDBProcType.PLUGIN, run, None)
        p.set_menu_label("Export PSD from XCF")
        p.add_menu_path("<Image>/Filters/My")   # 画像を開いている時に表示
        p.set_documentation(
            "Export PSD from XCF",
            "PSD形式でエクスポートするスクリプト",
            "my-export-psd.py",
        )
        p.set_attribution("Your Name", "Public Domain", "2025")
        # 画像が必要なメニュー配下なので image types を指定
        p.set_image_types("*")
        # 描画対象が選べる状態で有効化(最低限の感度)
        p.set_sensitivity_mask(Gimp.ProcedureSensitivityMask.DRAWABLE)
        return p
Gimp.main(RunExportPSD.__gtype__, sys.argv)

コメント