Search code examples
pythongimpwebp

Problem saving WebP with Gimp Python script


I have this script to automate resizing and saving into WebP format, but I have something set wrong:

!/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, quality=75, alpha_quality=100, lossless=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()

This is my error message:

Traceback (most recent call last):
  File "C:\Program Files\GIMP 2\lib\gimp\2.0\python/gimpfu.py", line 741, in response
    dialog.res = run_script(params)
  File "C:\Program Files\GIMP 2\lib\gimp\2.0\python/gimpfu.py", line 362, in run_script
    return apply(function, params)
  File "C:\Users\***\AppData\Roaming\GIMP\2.10\plug-ins\crop_rescale_resolution.py", line 23, in automate_edit
    pdb.file_webp_save(image, drawable, filename, filename, quality=75, alpha_quality=100, lossless=False)
TypeError: expecting at most one keyword argument

Any suggestion pls?

I want the script to resize and change resolution to my image. Pretty simple, and than save to WebP, it does all except the last part.

I tried serveral ways to edit the saving part, but it just wont go for me.


Solution

  • Why are you using keywords arguments in:

     pdb.file_webp_save(image, drawable, filename, filename, quality=75, alpha_quality=100, lossless=False)
    

    AFAIK the only accepted keyword argument is run_mode, all others are positional. You are also missing many arguments:

    enter image description here