pythongimppython-fu

How to save an image as a lossless webp using only GIMP's python-fu


I have a python-fu question. I am using GIMP 2.10.12, mac version.
Let's say I have an image open in GIMP

Original example image:

image

and I want to save it in webp format using python-fu. I do this:

img=gimp.image_list()[0]
layer=img.layers[0]
pdb.gimp_file_save(img,layer,'/my_path/my_image.webp','?')

It works, but when I check the resulting webp file turns out its quality has degraded. It's being saved with lossy compression.

Resulting lossy image:

image

If I do the same process with the GUI, in the dialog box there's the checkbox for lossless compression, which I checked, and the resulting file has the quality that I want.

Gimp dialog box with the lossless option checked:

image

My question is: How can I save an image as a webp file with the lossless compression option checked using only Python-fu, not the GUI?

I have tried:
-Flattening the image first to take away the alpha channel, didn't work.
-Looking in GIMP's developer documentation, but haven't found anything yet.
-Looking in the preferences panel for a way to leave the lossless option checked by default. Haven't found anything yet.

Am I missing something? Any pointers will be welcome. Thanks.


Solution

  • When you save an image with gimp-file-save Gimp determines the image type by looking at the extension you provide in the name, and saves it with the defaults settings that are set for this type. With some image types (JPEG & PNG), one solution is to change Gimp's default options, but there is no way to change them for WebP.

    However the general solution is that there are type-specific export/save functions (normally called file-${type}-save), and these have all the options shown in the export dialogs. For WebP you would use file-webp-save(*):

    enter image description here

    which, as you can see has a lossless option.

    I never used that one but word of warning: normally these calls only save a "drawable", i.e., in most cases, a single layer. If you have a multi-layer image and want to save the whole image, you have to flatten it (or save a flattened duplicate) or create a temporary layer to save using gimp_layer_new_from_visible).

    (*) If you haven't found it yet, Filters > Python-fu > Console gives you a console to try things, and in that window a Browse button sends you to the procedure browser where everything is documented (this doc is generated dynamically and takes in account all the plugins you may have added).