You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GIMP Python脚本保存WebP遇TypeError参数错误求助

问题解决:GIMP Python脚本保存WebP时的TypeError错误

错误原因

GIMP的PDB(Procedural Database)函数对关键字参数的支持存在限制,pdb.file_webp_save不允许同时传入多个关键字参数,必须使用位置参数传递可选参数。

修复方案

将保存WebP的代码中的关键字参数改为位置参数,按函数定义的参数顺序传递值即可。

修改后的核心代码片段

# 4. Save the image as a WebP file
filename = image.filename.rsplit('.', 1)[0] + '_edited.webp'
# 改用位置参数传递quality、alpha_quality、lossless参数
pdb.file_webp_save(image, drawable, filename, filename, 75, 100, False)

完整修复后的脚本

#!/usr/bin/env python
from gimpfu import *

def automate_edit(image, drawable, new_width, new_height):
    pdb.gimp_image_undo_group_start(image)

    # Get the selection bounds
    non_empty, x1, y1, x2, y2 = pdb.gimp_selection_bounds(image)

    # Check if a selection exists
    if non_empty:
        # 1. Crop the image based on the current selection
        pdb.gimp_image_crop(image, x2 - x1, y2 - y1, x1, y1)

        # 2. Rescale the image
        pdb.gimp_image_scale(image, new_width, new_height)

        # 3. Change the resolution to 72 DPI
        pdb.gimp_image_set_resolution(image, 72, 72)

        # 4. Save the image as a WebP file
        filename = image.filename.rsplit('.', 1)[0] + '_edited.webp'
        # 修复:使用位置参数传递可选参数
        pdb.file_webp_save(image, drawable, filename, filename, 75, 100, False)

        pdb.gimp_image_undo_group_end(image)
    else:
        pdb.gimp_message("Please make a selection before running the script.")

register(
    "python_fu_automate_edit",
    "Automate crop, rescale, change resolution and save",
    "Automate crop, rescale, change resolution and save",
    "Your Name",
    "Your Name",
    "2023",
    "<Image>/Filters/Automation/Automate Edit...",
    "RGB*, GRAY*",
    [
        (PF_INT, "new_width", "New width (pixels)", 800),
        (PF_INT, "new_height", "New height (pixels)", 1200),
    ],
    [],
    automate_edit)

main()

补充说明

如果需要调整更多WebP保存参数(如预设、EXIF保留、ICC配置文件等),可以通过GIMP的Python控制台输入help(pdb.file_webp_save)查看完整的参数顺序和默认值,按顺序添加位置参数即可。

内容的提问来源于stack exchange,提问作者Seehank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 14:47:08