Windows 上的 GIMP - 从命令行执行 python-fu 脚本

GIMP on Windows - executing a python-fu script from the command line

在 Windows 环境中,我想调用 GIMP 来执行 python-fu 脚本(通过 BAT 文件),但我使用的命令行调用没有产生预期的结果。

例如,考虑以下名为 makeafile_and_quit.py 的 python-fu 脚本,它位于我的 GIMP 的 plug-ins 文件夹中。其目的是加载现有图像并以不同的名称保存:

#!/usr/bin/env python

# Sample call from GIMP's python-fu console:
# pdb.python_fu_makeafile_and_quit_script()

from gimpfu import *

def makeafile_and_quit( ) :

    FILEPATH   = 'C:\path\to\file.JPG'
    IMAGE      = pdb.gimp_file_load( FILEPATH,  FILEPATH )

    pdb.gimp_file_save( IMAGE, pdb.gimp_image_get_active_drawable( IMAGE ), FILEPATH + '_2.jpg',  FILEPATH + '_2.jpg' )

    pdb.gimp_quit(0)

    return


# PLUGIN REGISTRATION
# This is the plugin registration function
register(
    'makeafile_and_quit_script',
    'v0.0',
    'A new concept',
    'Author',
    'Author',
    'Just now',
    '<Toolbox>/MyScripts/This will make a file and _QUIT',
    '',
    [],
    [],
    makeafile_and_quit
    )

main()

如果从 GIMP 的 'GUI instance' 调用脚本,脚本将完美执行,通过菜单调用脚本。它会在与源文件相同的文件夹中生成一个以“_2.jpg”结尾的新文件。

使用以下命令从命令提示符调用时行为不同:

"C:\Program Files\GIMP 2\bin\gimp-2.8.exe" --batch '("makeafile_and_quit.py")' -b "(gimp-quit 0)"

创建了一个 GIMP 实例,然后关闭但没有创建文件,即使看到了消息 batch command executed successfully

如何从命令行重复与 'GUI instance' 完全相同的行为?

来自我的 Windows 档案:

gimp -idf -b "yourscript" -b "pdb.gimp_quit(1)"

经过多次摆弄,我得出了以下命令,它可以按预期工作:

"C:\Program Files\GIMP 2\bin\gimp-console-2.8.exe" --verbose --batch "(python-fu-makeafile-and-quit-script RUN-NONINTERACTIVE)" --batch "(gimp-quit 0)"

注意:

  • 使用gimp-console-2.8.exe代替gimp-2.8.exe以避免执行结束时不必要的击键
  • 在函数名称前加上python-fu-
  • 前缀
  • 在名称中使用 - 而不是 _
  • 添加通用的(必要的)RUN-NONINTERACTIVE 参数
  • 在您的脚本中,不要使用调用显示的函数,例如 DISPLAY = gimp.Display( IMAGE ),这会使脚本失败 gimp-console-2.8.exe