#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2025/12/09 13:51:17 +0900> """ Windowsのファイル選択ダイアログから画像を開くPython-Fuプラグイン ファイル > Win Open にメニュー項目が追加される。 GIMP 2.10用。 Author : mieki256 Windows11 x64 25H2 + GIMP 2.10.38 Portable """ from gimpfu import * import ctypes import os WINOPEN_FILTERS = [ "All files {*.*}", "*.*", "PNG", "*.png", "JPEG", "*.jpg;*.jpeg", ] OFN_ALLOWMULTISELECT = 0x00000200 OFN_FILEMUSTEXIST = 0x00001000 OFN_PATHMUSTEXIST = 0x00000800 OFN_EXPLORER = 0x00080000 BUFFER_SIZE = 32768 class OPENFILENAME_WINDLG(ctypes.Structure): """Windowsの標準ファイルダイアログ用の構造体を定義""" _fields_ = [ ("lStructSize", ctypes.c_uint32), ("hwndOwner", ctypes.c_void_p), ("hInstance", ctypes.c_void_p), ("lpstrFilter", ctypes.c_wchar_p), ("lpstrCustomFilter", ctypes.c_wchar_p), ("nMaxCustFilter", ctypes.c_uint32), ("nFilterIndex", ctypes.c_uint32), ("lpstrFile", ctypes.c_wchar_p), ("nMaxFile", ctypes.c_uint32), ("lpstrFileTitle", ctypes.c_wchar_p), ("nMaxFileTitle", ctypes.c_uint32), ("lpstrInitialDir", ctypes.c_wchar_p), ("lpstrTitle", ctypes.c_wchar_p), ("Flags", ctypes.c_uint32), ("nFileOffset", ctypes.c_uint16), ("nFileExtension", ctypes.c_uint16), ("lpstrDefExt", ctypes.c_wchar_p), ("lCustData", ctypes.c_void_p), ("lpfnHook", ctypes.c_void_p), ("lpTemplateName", ctypes.c_wchar_p), ("pvReserved", ctypes.c_void_p), ("dwReserved", ctypes.c_uint32), ("FlagsEx", ctypes.c_uint32), ] def get_openfilename_windlg(initial_dir=None, filters=None): """Windowsの標準ファイルダイアログを使って複数ファイルを選択""" comdlg32 = ctypes.WinDLL("comdlg32") comdlg32.GetOpenFileNameW.restype = ctypes.c_bool comdlg32.GetOpenFileNameW.argtypes = (ctypes.POINTER(OPENFILENAME_WINDLG),) ofn = OPENFILENAME_WINDLG() lenFilenameBufferInChars = BUFFER_SIZE buf = ctypes.create_unicode_buffer(lenFilenameBufferInChars) ofn.lStructSize = ctypes.sizeof(OPENFILENAME_WINDLG) # ファイル種類を設定 if filters: ofn.lpstrFilter = "\0".join(filters) + "\0\0" else: ofn.lpstrFilter = "All files {*.*}\0*.*\0\0" if initial_dir: ofn.lpstrInitialDir = initial_dir ofn.lpstrFile = ctypes.cast(buf, ctypes.c_wchar_p) ofn.nMaxFile = lenFilenameBufferInChars ofn.lpstrTitle = "Select file" ofn.Flags = ( OFN_ALLOWMULTISELECT | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST ) # ファイル選択ダイアログを開く ret = comdlg32.GetOpenFileNameW(ofn) if ret: # "\0" を区切り文字とした文字列が得られるので分割してリストにする s = buf[:].rstrip("\0") path = s[: ofn.nFileOffset].rstrip("\0") filenames = s[ofn.nFileOffset :].split("\0") files = [os.path.abspath(os.path.join(path, f)) for f in filenames] # ファイルパスをリストで返す return files return [] def python_fu_plugin_m256_win_open(): """Windowsのファイル選択ダイアログを経由して複数画像ファイルを開く""" # 画像を開いてない状態でも動作させるので関数に渡す引数は無い initdir = os.path.expanduser("~") files = get_openfilename_windlg(initial_dir=initdir, filters=WINOPEN_FILTERS) if files: # ファイルが選択された for filepath in files: # "\\" は問題があるらしいので "/" に置換 path = filepath.replace(os.sep, "/") # 画像を読み込む image = pdb.gimp_file_load(path, path) if image: gimp.Display(image) # GIMPウインドウに画像を表示する pdb.gimp_image_clean_all(image) # 編集中フラグをクリア else: gimp.message("Image loading failed. [%s]" % (path)) else: # キャンセルされた pass gimp.displays_flush() # GIMPプラグインの情報を指定 register( "python-fu-m256-win-open", # プロシージャ名 "Open Windows file select dialog", # 簡易説明 "Open image with Windows file select dialog", # 詳細説明 "mieki256", # 作者名 Author "mieki256", # 著作権者名 Copyright "2025/12/07", # 作成日 # メニューの表示場所に Toolbox を指定、かつ、画像種類を "" にすることで、 # 画像を開いていなくても利用できるようにするらしい。 "Win Open", # メニューラベル名 "", # 画像種類 [], # 引数 [], # 返り値 python_fu_plugin_m256_win_open, # 関数名 menu="/File", # メニューの表示場所 ) main()