简体   繁体   English

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

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

In a Windows environment, I would like to make a call to GIMP for executing a python-fu script (through a BAT file) but the command line call I am using does not produce the expected results.在 Windows 环境中,我想调用 GIMP 来执行 python-fu 脚本(通过 BAT 文件),但是我使用的命令行调用没有产生预期的结果。

For example, consider the following python-fu script named makeafile_and_quit.py , which rerside in my GIMP's plug-ins folder.例如,考虑以下名为makeafile_and_quit.py的 python-fu 脚本,它位于我的 GIMP plug-ins文件夹中。 Its purpose is to load an existing image and save under a different name:其目的是加载现有图像并以不同的名称保存:

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

The script executes flawlessly if called from a 'GUI instance' of GIMP, calling the script through the menus.如果从 GIMP 的“GUI 实例”调用脚本,脚本将完美执行,通过菜单调用脚本。 It produces a new file ending with '_2.jpg' in the same folder as the source file.它会在与源文件相同的文件夹中生成一个以“_2.jpg”结尾的新文件。

The behaviour is different when called from the command prompt using the following:使用以下命令从命令提示符调用时行为不同:

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

An instance of GIMP is created, then, closes but no file is created even though the message batch command executed successfully is seen.创建一个 GIMP 实例,然后关闭但没有创建文件,即使看到batch command executed successfully也是如此。

How can I repeat exactly the same behaviour as a 'GUI instance', from the command line?如何从命令行重复与“GUI 实例”完全相同的行为?

After much fiddling, I arrived at the following command which works as desired: 经过多次摆弄后,我得到了以下命令:

"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)" “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)“

Take care to: 注意:

  • use gimp-console-2.8.exe instead of gimp-2.8.exe to avoid unnecessary keystroke at the end of execution 使用gimp-console-2.8.exe而不是gimp-2.8.exe来避免在执行结束时不必要的击键
  • prefix the function name with python-fu- 使用python-fu-为函数名添加前缀
  • use - 's instead of _ 's in names 在名称中使用-而不是_
  • add the generic (and necessary) RUN-NONINTERACTIVE argument 添加通用(和必要) RUN-NONINTERACTIVE参数
  • in your script, do not use functions calling displays, such as DISPLAY = gimp.Display( IMAGE ) , which make the script fail with gimp-console-2.8.exe 在你的脚本中,不要使用调用显示的函数,例如DISPLAY = gimp.Display( IMAGE ) ,这会使脚本失败并使用gimp-console-2.8.exe

从我的Windows档案:

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

it's been a while.有一阵子了。 Nevertheless i want to do the same thing.不过我想做同样的事情。 The difference is, that my script in pyhton is using two variables.不同之处在于,我在 pyhton 中的脚本使用了两个变量。

import os
from gimpfu import *

## The image has to be manually opened in gimp.
## under File -> Crop and Merge the Script can be executed
## The file is safed in the same location in an subdirectory that can be changed in the script below

dir_name = '\\Homepage\\'

def my_function(image, drawable):
    # define variables
    drawable = pdb.gimp_image_get_active_drawable(image)
    width = float(pdb.gimp_image_width(image))
    height = float(pdb.gimp_image_height(image))
    image_duplicate = pdb.gimp_image_duplicate(image)
    aspect_ratio = 1520./980.
    watermark_file = "F:\Vorlage_Homepage_1520x980.png"
    counter = 1
    img_file = pdb.gimp_image_get_filename(image)
    img_path = os.path.dirname(img_file)
    print (img_path)

    #checking, if a subdirectory exists
    check_path = img_path + dir_name
    exist = os.path.exists(check_path)
    if not exist:
        os.makedirs (check_path)

    #checking, if file(s) already exists
    exists = 1
    while exists:
        if os.path.exists(check_path + os.path.basename(os.path.dirname(img_file)) + '_' + str(counter) + '.jpg'):
            counter += 1
        else:
            exists = 0

    export_name = check_path + os.path.basename(os.path.dirname(img_file)) + '_' + str(counter) + '.jpg'
    print (export_name)    

    if width/height > aspect_ratio:
        # crop the image centerd by width
        new_width = height * aspect_ratio
        off_x = int((width - new_width) / 2)
        pdb.gimp_image_crop(image_duplicate, int(new_width), int(height), off_x, 0)
    else:
        # crop the image centerd by height
        new_height = width / aspect_ratio
        off_y = int((height-new_height) / 2)
        pdb.gimp_image_crop(image_duplicate, int(width), int(new_height), 0, off_y)

    # Scaling the image
    pdb.gimp_image_scale(image_duplicate, 1520, 980)

    # add the watermark
    layer_to_add = pdb.gimp_file_load_layer(image_duplicate, watermark_file)
    pdb.gimp_image_add_layer(image_duplicate, layer_to_add, 0)

    # merge layers
    merged_layer = pdb.gimp_image_merge_visible_layers(image_duplicate, 2)
    pdb.gimp_image_set_active_layer(image_duplicate, merged_layer)
    pdb.file_jpeg_save(image_duplicate, merged_layer, export_name, export_name, 0.85, 0, 1, 1, "", 0, 1, 0, 0)



register(
    "python_fu_my_function",  # Name of the function
    "Crop and Merge",  # Description of the function
    "Crops and scales the image, aftwards give it a watermark",  # Help text
    "Sebastian Knab",  # Author
    "Sebastian Knab",  # Copyright
    "2023",  # Creation date
    "<Image>/File/Crop and Merge",  # Menu location
    "*",  # Image type
    [],  # Input parameters
    [],  # Return values
    my_function,  # The function to register
)

main()

The code works fine in gimp.该代码在 gimp 中运行良好。 Interesting is that I have to use drawable, even I don't use it.有趣的是我必须使用drawable,即使我不使用它。 But if i want to call it via a.bat script nothing happens, with --verbose I don't get an error, just the hint that the default interpreter is used.但是,如果我想通过 a.bat 脚本调用它,则不会发生任何事情,使用 --verbose 我不会收到错误,只是提示使用了默认解释器。

setlocal enabledelayedexpansion
set "edited_photos_file=F:\edited_photos.txt"
set "script_path=C:\Users\sebas\AppData\Roaming\GIMP\2.10\plug-ins"
set "gimp=C:\Program Files\GIMP 2\bin\gimp-console-2.10.exe"
set "root_dir=F:\test"

rem create a list of edited photos if it doesn't exist
if not exist %edited_photos_file% type nul > %edited_photos_file%

rem search for images in current directory and subdirectories
for /R "%root_dir%" %%f in (*.jpg *.png *.bmp) do (
    set "image_file=%%f"
    echo !image_file!

    rem check if the file has already been edited
    for /F "delims=" %%l in ("%edited_photos_file%") do (
        if "%%l" == !image_file! (
            :: Image file has already been edited, skip it
            echo !image_file! already edited
            goto :continue
        )
    )
    :: Image file has not been edited, execute the script on it
    echo Executing script on !image_file!
    timeout /t 2
    "%gimp%"  --verbose --batch python-fu-eval "(python-fu-my-function '!image_file!' 'None')" --batch "(gimp-quit 0)"
    rem add the file to the list of edited photos
    echo !image_file! >> %edited_photos_file%

    :continue
    echo edit new file or finish
)

Here is what I get in the.log file:这是我在 .log 文件中得到的:

Initializing plug-in: 'C:\Program Files\GIMP 2\lib\gimp\2.0\plug-ins\file-rawtherapee\file-rawtherapee.exe' Initializing plug-in: 'C:\Program Files\GIMP 2\lib\gimp\2.0\plug-ins\file-heif\file-heif.exe' Initializing plug-in: 'C:\Program Files\GIMP 2\lib\gimp\2.0\plug-ins\file-darktable\file-darktable.exe' Starting extension: 'extension-script-fu' No batch interpreter specified, using the default 'plug-in-script-fu-eval'.初始化插件:'C:\Program Files\GIMP 2\lib\gimp\2.0\plug-ins\file-rawtherapee\file-rawtherapee.exe' 初始化插件:'C:\Program Files\GIMP 2\ lib\gimp\2.0\plug-ins\file-heif\file-heif.exe' 初始化插件:'C:\Program Files\GIMP 2\lib\gimp\2.0\plug-ins\file-darktable\file -darktable.exe' 起始扩展名:'extension-script-fu' 未指定批处理解释器,使用默认的'plug-in-script-fu-eval'。 batch command executed successfully EXIT: gimp_exit EXIT: gimp_real_exit Writing 'C:\Users\sebas\AppData\Roaming\GIMP\2.10\colorrc' Writing 'C:\Users\sebas\AppData\Roaming\GIMP\2.10\templaterc' Writing 'C:\Users\sebas\AppData\Roaming\GIMP\2.10\parasiterc' Writing 'C:\Users\sebas\AppData\Roaming\GIMP\2.10\unitrc' EXIT: app_exit_after_callback edit new file or finish批处理命令执行成功 EXIT: gimp_exit EXIT: gimp_real_exit Writing 'C:\Users\sebas\AppData\Roaming\GIMP\2.10\colorrc' Writing 'C:\Users\sebas\AppData\Roaming\GIMP\2.10\templaterc' Writing ' C:\Users\sebas\AppData\Roaming\GIMP\2.10\parasiterc' Writing 'C:\Users\sebas\AppData\Roaming\GIMP\2.10\unitrc' EXIT: app_exit_after_callback 编辑新文件或完成

Of course i tried a lot with the arguments by calling the gimp-console-2.10.exe.当然,我通过调用 gimp-console-2.10.exe 对 arguments 进行了很多尝试。 After a lot hours i thought maybe someone can help.几个小时后,我想也许有人可以提供帮助。 It also doesn't help to run the.bat with admin rights.以管理员权限运行.bat 也无济于事。 The images are not saved.图像未保存。 I also tried the syntax form this site.我还尝试了这个站点的语法。

"%gimp%" --verbose --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import script;script.einser('!image_file!', '!image_file!')" -b "pdb.gimp_quit(1)"

I tried the second argument with 'None' or '', but this have no influence.我用“无”或“”尝试了第二个参数,但这没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM