https://github.com/HugoSmits86/nativewebp
Native webp encoder for Go
https://github.com/HugoSmits86/nativewebp
Last synced: 9 months ago
JSON representation
Native webp encoder for Go
- Host: GitHub
- URL: https://github.com/HugoSmits86/nativewebp
- Owner: HugoSmits86
- License: mit
- Created: 2024-12-25T12:18:02.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-26T14:16:59.000Z (11 months ago)
- Last Synced: 2025-04-26T15:19:51.588Z (11 months ago)
- Language: Go
- Size: 80.1 KB
- Stars: 220
- Watchers: 4
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - nativewebp - Go native WebP encoder with zero external dependencies. (Images / Search and Analytic Databases)
- awesome-go-with-stars - nativewebp - 11-17 | (Images / Search and Analytic Databases)
- fucking-awesome-go - nativewebp - Go native WebP encoder with zero external dependencies. (Images / Search and Analytic Databases)
- awesome-go - HugoSmits86/nativewebp
- awesome-go - nativewebp - Go native WebP encoder with zero external dependencies. (Images / Search and Analytic Databases)
- awesome-go-cn - nativewebp
README
[](https://codecov.io/gh/HugoSmits86/nativewebp)
[](https://pkg.go.dev/github.com/HugoSmits86/nativewebp)
[](https://opensource.org/licenses/MIT)
# Native WebP for Go
This is a native WebP encoder written entirely in Go, with **no dependencies on libwebp** or other external libraries. Designed for performance and efficiency, this encoder generates smaller files than the standard Go PNG encoder and is approximately **50% faster** in execution.
Currently, the encoder supports only WebP lossless images (VP8L).
## Decoding Support
We provide WebP decoding through a wrapper around `golang.org/x/image/webp`, with an additional `DecodeIgnoreAlphaFlag` function to handle VP8X images where the alpha flag causes decoding issues.
## Benchmark
We conducted a quick benchmark to showcase file size reduction and encoding performance. Using an image from Google’s WebP Lossless and Alpha Gallery, we compared the results of our nativewebp encoder with the standard PNG encoder.
For the PNG encoder, we applied the `png.BestCompression` setting to achieve the most competitive compression outcomes.
PNG encoder
nativeWebP encoder
reduction

file size
120 kb
96 kb
20% smaller
encoding time
42945049 ns/op
27716447 ns/op
35% faster

file size
46 kb
36 kb
22% smaller
encoding time
98509399 ns/op
31461759 ns/op
68% faster

file size
236 kb
194 kb
18% smaller
encoding time
178205535 ns/op
102454192 ns/op
43% faster

file size
53 kb
41 kb
23% smaller
encoding time
29088555 ns/op
14959849 ns/op
49% faster

file size
139 kb
123 kb
12% smaller
encoding time
63423995 ns/op
21717392 ns/op
66% faster
image source: https://developers.google.com/speed/webp/gallery2
## Installation
To install the nativewebp package, use the following command:
```Bash
go get github.com/HugoSmits86/nativewebp
```
## Usage
Here’s a simple example of how to encode an image:
```Go
file, err := os.Create(name)
if err != nil {
log.Fatalf("Error creating file %s: %v", name, err)
}
defer file.Close()
err = nativewebp.Encode(file, img, nil)
if err != nil {
log.Fatalf("Error encoding image to WebP: %v", err)
}
```
Here’s a simple example of how to encode an animation:
```Go
file, err := os.Create(name)
if err != nil {
log.Fatalf("Error creating file %s: %v", name, err)
}
defer file.Close()
ani := nativewebp.Animation{
Images: []image.Image{
frame1,
frame2,
},
Durations: []uint {
100,
100,
},
Disposals: []uint {
0,
0,
},
LoopCount: 0,
BackgroundColor: 0xffffffff,
}
err = nativewebp.EncodeAll(file, &ani, nil)
if err != nil {
log.Fatalf("Error encoding WebP animation: %v", err)
}
```