Loading a *.webp image file

Youtube (and by extension youtube-dl) recently started to give thumbnails in the *.webp format, instead of the *.jpg format. If you try to load the *.webp files into a gdk.Pixbuf using the gdk.PixbufNewFromFile() function you get an error:

Couldn’t recognize the image file format for file “4kberLzSvx0.webp”

So I started to look at the package golang.org/x/image/webp (https://pkg.go.dev/golang.org/x/image/webp?tab=doc) but I don’t know if this package is what I am supposed to use to enable webp format support, or how to use it properly.

Does anyone here know how to load an image in webp format?

You can see my code at:

This is my test code so far:

package main

import (
	"fmt"
	"github.com/gotk3/gotk3/gdk"
	_ "golang.org/x/image/webp"
)

func main() {
	// Jpeg works fine
	loadImage("031wc2m0IWs.jpg")

	// Webp does not work
	loadImage("4kberLzSvx0.webp")
}

func loadImage(path string) *gdk.Pixbuf {

	image, err := gdk.PixbufNewFromFile(path)
	if err!=nil {
		fmt.Printf("%s", err.Error())
	}
	return image
}

I’d guess gdk.PixbufNewFromFile doesn’t use the Go image loading libraries, rather it uses its own C versions.

You could load the image and convert the format in Go before passing it to gdk - that is probably your best option.

You mean load the file as you load any binary file, and then convert it to an Image using the decode method in the webp-package, and from there to a PixBuf? I did not think of doing that, but I will try it…

Ok, that works, so now I have an image.Image and need to convert it to a gdk.PixBuf, which I can’t seem to do. But that is probably a question more suited for the GoTK3 team, so thanks for the help.

1 Like

If you just want to get things going, you could save it as a temporary file and delete it again after…

My backup solution, will be to to do something like that (but I will save it as a jpg and keep it until the user deletes the video, instead of deleting it immediately), but I would prefer to just read the webp, and not have to do the conversion thing. Hopefully the GoTK3-gang will suggest a solution, where I don’t need to convert. There might already be a function for that, that I just don’t know about.

It seems like GdkPixBuf does not support webp images. See response by GoTK3 here:

But there are some workarounds, including doing a conversion, by loading the image using the package mentioned above, and saving it down as a jpg, as we talked about above.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.