imgdiet is a Go module built for optimizing images. It leverages the
power of the libvips library to
provide an easy-to-use, lightweight, and idiomatic way to reduce image
size without significant loss of quality.
You'll need to have Go v1.25 and libvips v8.18 installed on your
system to use imgdiet.
To install imgdiet and use it in your project, run:
go get git.sr.ht/~jamesponddotco/imgdiet-go@latestPlease see the Go reference documentation.
package main
import (
"context"
"log"
"os"
"git.sr.ht/~jamesponddotco/imgdiet-go"
)
func main() {
// Start libvips with our default settings.
imgdiet.Start(nil)
defer imgdiet.Stop()
// Open an image from a file as an io.Reader.
file, err := os.Open("/path/to/image.jpg")
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Open the image for processing.
img, err := imgdiet.Open(context.Background(), file)
if err != nil {
log.Fatal(err)
}
// Export the image with default optimization settings for the web, and as a
// WebP image.
exported, err := img.Export(imgdiet.FormatWebP, imgdiet.DefaultOptions())
if err != nil {
log.Fatal(err)
}
// Check to see how many bytes were saved.
log.Printf("Saved %d bytes.", img.Size()-int64(len(exported)))
}Anyone can help make imgdiet better. Send patches to the mailing
list and report bugs
on the issue tracker.
You must sign-off your work using git commit --signoff. Follow the
Linux kernel developer's certificate of
origin
for more details.
All contributions are made under the MIT license.
The following resources are available:
- Support and general discussions.
- Patches and development related questions.
- Instructions on how to prepare patches.
- Feature requests and bug reports.
Released under the MIT License.