簡體   English   中英

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 實例”調用腳本,腳本將完美執行,通過菜單調用腳本。 它會在與源文件相同的文件夾中生成一個以“_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 實例”完全相同的行為?

經過多次擺弄后,我得到了以下命令:

“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

從我的Windows檔案:

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

有一陣子了。 不過我想做同樣的事情。 不同之處在於,我在 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()

該代碼在 gimp 中運行良好。 有趣的是我必須使用drawable,即使我不使用它。 但是,如果我想通過 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
)

這是我在 .log 文件中得到的:

初始化插件:'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'。 批處理命令執行成功 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 編輯新文件或完成

當然,我通過調用 gimp-console-2.10.exe 對 arguments 進行了很多嘗試。 幾個小時后,我想也許有人可以提供幫助。 以管理員權限運行.bat 也無濟於事。 圖像未保存。 我還嘗試了這個站點的語法。

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

我用“無”或“”嘗試了第二個參數,但這沒有影響。

暫無
暫無

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

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