Search code examples
schemegimp

Best way to export an image in multiple formats using GIMP's batch scripting in the same script?


I am trying to automate creating copies of a GIMP image in different formats, in order to serve them on a website (Using the picture element, offering several versions of an image for compatibility). So I've been trying to build a script to do this so I don't have to manually export each image, as well as enable myself to define my own defaults for images.

So far, I have been able to cobble together a script that does accomplish most of my goals:

(define (hugo-image-processing filename)
  (
    let* (
      (image (car (gimp-file-load 1 (string-append filename ".xcf") (string-append filename ".xcf"))))
      (drawable (car (gimp-image-get-active-layer image))))
    (file-png-save-defaults 1 image drawable (string-append filename ".png") (string-append filename ".png"))
    (file-heif-av1-save 1 image drawable (string-append filename ".avif") (string-append filename ".avif") 100 1)
    (file-webp-save 1 image drawable (string-append filename ".webp") (string-append filename ".webp") 0 1 100 100 1 1 1 1 0 0 0 1 1)
    (gimp-image-delete image)
  )
)

This works, and image are exported in expected formats, but I noticed that the size of my output files vary based on the order that the images are saved. In the above case, my test image exports as an AVIF file at 139 kB while my WebP file weighs in at 76 kB. If I swap the order, and save the AVIF file after the WebP file, the WebP file still contains 76 kB, but the AVIF file is now 58 kB in size.

This behavior can be seen if one moves the file-png-save-defaults as well (though WebP stays a consistent 76 kB in any order).

I'm guessing that the file--save commands are modifying either the image or the drawable, but I haven't quite figured out why. I'm not terribly familiar with Scheme, so my ability to troubleshoot this beyond observing this behavior is limited. Any input on this issue would be appreciated.

I run this command gimp-2.10 -i -b "(hugo-image-processing \"working\")" -b "(gimp-quit 0)" to call the script, and I am using Gimp 2.10.30 on Windows 11.


Solution

  • As per the comments, the problem appears to come from a spurious "save as animation" flag in the file-webp-save call. Possibly a bug in 2.10.30, that I cannot reproduce in my 2.10.24, even with the animation flag.

    I tested the problem with the following code:

    def fullname(name,extension):
        return "/tmp/"+name+"."+extension
    
    def savemulti(image):
        xcf=fullname(image,"xcf")
        png=fullname(image,"png")
        avif=fullname(image,"avif")
        webp=fullname(image,"webp")
        image=pdb.gimp_file_load(xcf,xcf)
        drawable=image.active_layer
        pdb.file_png_save_defaults(image,drawable,png,png)
        pdb.file_webp_save(image,drawable,webp,webp,0, 1, 100, 100,1, 1, 1, 1, 0, 0, 0, 1, 1)
        pdb.file_heif_save(image,drawable,avif,avif,100,1)
        gimp.Display(image) # Start an image window
    

    (which at the end start a Gimp window with the image in it instead of deleting the image).

    Copy-paste in the python-fu console, after editing the fullname(...) function, and invoke as savemulti("TestImage"). When the windows comes up, you can check the Layers list and see if there are extra layers by any chance...