Simple and fast webp library for golang

logo

go-webp

Build Status GoDoc Go Report codecov

Golang Webp library for encoding and decoding, using C binding for Google libwebp

Requirements

libwebp

Benchmarks

% go test -bench "^BenchmarkDecode" ./webp                                                                                
goos: darwin
goarch: amd64
pkg: github.com/kolesa-team/go-webp/webp
BenchmarkDecodeLossy-12                       45          25965139 ns/op
BenchmarkDecodeXImageLossy-12                 13          90735879 ns/op
BenchmarkDecodeLossless-12                    64          18887482 ns/op
BenchmarkDecodeXImageLossless-12              27          42422596 ns/op
PASS
ok      github.com/kolesa-team/go-webp/webp     7.877s

Install libwebp

MacOS:

brew install webp

Linux:

sudo apt-get update
sudo apt-get install libwebp-dev

Install

go get -u github.com/kolesa-team/go-webp

Examples

Decode:

package main

import (
	"image/jpeg"
	"log"
	"os"

	"github.com/kolesa-team/go-webp/decoder"
	"github.com/kolesa-team/go-webp/webp"
)

func main() {
	file, err := os.Open("test_data/images/m4_q75.webp")
	if err != nil {
		log.Fatalln(err)
	}

	output, err := os.Create("example/output_decode.jpg")
	if err != nil {
		log.Fatal(err)
	}
	defer output.Close()

	img, err := webp.Decode(file, &decoder.Options{})
	if err != nil {
		log.Fatalln(err)
	}

	if err = jpeg.Encode(output, img, &jpeg.Options{Quality:75}); err != nil {
		log.Fatalln(err)
	}
}
go run example/decode/main.go

Encode

package main

import (
	"github.com/kolesa-team/go-webp/encoder"
	"github.com/kolesa-team/go-webp/webp"
	"image/jpeg"
	"log"
	"os"
)

func main() {
	file, err := os.Open("test_data/images/source.jpg")
	if err != nil {
		log.Fatalln(err)
	}

	img, err := jpeg.Decode(file)
	if err != nil {
		log.Fatalln(err)
	}

	output, err := os.Create("example/output_decode.webp")
	if err != nil {
		log.Fatal(err)
	}
	defer output.Close()

	options, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, 75)
	if err != nil {
		log.Fatalln(err)
	}

	if err := webp.Encode(output, img, options); err != nil {
		log.Fatalln(err)
	}
}
go run example/encode/main.go

TODO

  • return aux stats
  • container api
  • incremental decoding

License

MIT licensed. See the LICENSE file for details.

Comments
  • Scale options requiring both X and Y

    Scale options requiring both X and Y

    Greetings!

    Great project. I ran into an issue today using the scale option for decoding a webp image. Is it possible to only scale the X of an image and have Y scale to preserve aspect ratio?

  • Build error with cgo_enabled=1 GOOS=linux GOARCH=amd64

    Build error with cgo_enabled=1 GOOS=linux GOARCH=amd64

    Build error with cgo_enabled=1 GOOS=linux GOARCH=amd64. How to solve this issue?

    github.com/kolesa-team/go-webp/encoder
    187 | /go/pkg/mod/github.com/kolesa-team/[email protected]/encoder/options.go:112:5: could not determine kind of name for C.WebPConfigLosslessPreset
    188 | # github.com/kolesa-team/go-webp/decoder
    189 | /go/pkg/mod/github.com/kolesa-team/[email protected]/decoder/decoder.go:111:48: d.config.input.format undefined (type _Ctype_struct_WebPBitstreamFeatures has no field or method format)
    190 | /go/pkg/mod/github.com/kolesa-team/[email protected]/decoder/options.go:82:16: config.options.dithering_strength undefined (type _Ctype_struct_WebPDecoderOptions has no field or method dithering_strength)
    191 | /go/pkg/mod/github.com/kolesa-team/[email protected]/decoder/options.go:85:17: config.options.flip undefined (type _Ctype_struct_WebPDecoderOptions has no field or method flip)
    192 | /go/pkg/mod/github.com/kolesa-team/[email protected]/decoder/options.go:88:16: config.options.alpha_dithering_strength undefined (type _Ctype_struct_WebPDecoderOptions has no field or method alpha_dithering_strength)
    
  • cannot fetch features: VP8_STATUS_BITSTREAM_ERROR

    cannot fetch features: VP8_STATUS_BITSTREAM_ERROR

    I'm trying to convert png to webp and getting error "cannot fetch features: VP8_STATUS_BITSTREAM_ERROR".

    func (c *ConvertService) ConvertToWebp(file io.Reader, d entities.Dimensions) (image.Image, error) {
    	img, err := webp.Decode(file, &decoder.Options{})
    
    	if err != nil {
    		return nil, err
    	}
    
    	return transform.Resize(img, d.Width, d.Height, transform.Linear), nil
    }
    

    OS: macOS Catalina, 10.15.7 Lib: webp 1.2.1 Go: 1.17

  • fatal error: 'webp/encode.h' file not found

    fatal error: 'webp/encode.h' file not found

    Hello,I'm using M1 MacBook Pro,MacOs Monterey.And I have installed webp package, but when I try to run this lib,I got this error:

    # github.com/kolesa-team/go-webp/encoder
    ../../../go/pkg/mod/github.com/kolesa-team/[email protected]/encoder/encoder.go:27:10: fatal error: 'webp/encode.h' file not found
    #include <webp/encode.h>
             ^~~~~~~~~~~~~~~
    1 error generated.
    # github.com/kolesa-team/go-webp/decoder
    ../../../go/pkg/mod/github.com/kolesa-team/[email protected]/decoder/decoder.go:27:10: fatal error: 'webp/decode.h' file not found
    #include <webp/decode.h>
             ^~~~~~~~~~~~~~~
    1 error generated.
    

    I installed webp lib with brew, and I found header files in this path,but I don't know how to link it:

    /opt/homebrew/include/webp
    

    Some one says that new MacOS seemed to have stopped using /usr/local/include/. See this link I'm not proficient in cgo and C,I don't known how to change the path to let cgo link these header files.I try to google but got no answer.Can you help me to solve this problem?Thank you!

  • Options.GetConfig handles nil caller gracefully

    Options.GetConfig handles nil caller gracefully

    Main goal: Make webp.Encode behave the same way as the first-party image-type Encode methods; if it receives nil options, use defaults rather than panicking or returning an error. I'm not opinionated on whether this is the correct defaults (and in fact don't know what settings PresetDefault corresponds to).

  • fatal error: 'webp/decode.h' file not found

    fatal error: 'webp/decode.h' file not found

    Hi there,

    I've just installed go-webp through the following steps on a MacOS M1:

    1 - brew install webp 2 - go get -u github.com/kolesa-team/go-webp

    When I try to run the demo, I get the following error:

    .../github.com/kolesa-team/[email protected]/decoder/decoder.go:27:10: fatal error: 'webp/decode.h' file not found
    #include <webp/decode.h>
             ^~~~~~~~~~~~~~~
    1 error generated.
    # github.com/kolesa-team/go-webp/encoder
    .../go/pkg/mod/github.com/kolesa-team/[email protected]/encoder/encoder.go:27:10: fatal error: 'webp/encode.h' file not found
    #include <webp/encode.h>
             ^~~~~~~~~~~~~~~
    1 error generated.
    
    

    Thanks in advance.

  • Failed to compile from Windows.

    Failed to compile from Windows.

    I'm trying to run an application in Go that converts image files to Webp format from Windows, but I get the following C compiler error. This problem doesn't happen on Linux, but I need to run it on Windows:

    C:\Program Files\Go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1 C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lwebp C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lwebp C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lwebp C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lwebp collect2.exe: error: ld returned 1 exit status

    temp

    UPDATE: I solved the above problem by changing the codeblocks compiler to w64devkit, but now it shows the following errors:

    Captura

  • Adds compatibility with the std image lib

    Adds compatibility with the std image lib

    Adding import _ "github.com/kolesa-team/go-webp" to a file will allow image.Decode to automatically detect and decode WebP files using this library.

    If this seems interesting/useful, I'll add tests and adapt where needed 😊

    Replicates the approach used in golang.org/x/image/webp.

  • Decoder.GetFeatures().HasAnimation() untested

    Decoder.GetFeatures().HasAnimation() untested

    Noticed this while looking to make use of this package. Patch suggested by inspection, but not run yet.

    *** decoder_test.go.orig 2022-01-21 13:39:38.000000000 -0500 --- decoder_test.go 2022-01-21 13:40:22.000000000 -0500


    *** 100,106 **** t.Fatal("file has_alpha is invalid") }

    ! if features.HasAlpha { t.Fatal("file has_animation is invalid") } } --- 100,106 ---- t.Fatal("file has_alpha is invalid") }

    ! if features.HasAnimation { t.Fatal("file has_animation is invalid") } }

Simple-Weather-API - Simple weather api app created using golang and Open Weather API key
Simple-Weather-API - Simple weather api app created using golang and Open Weather API key

Simple Weather API Simple weather api app created using golang and Open Weather

Feb 6, 2022
A small, fast, reliable pastemyst API wrapper written in Golang

A small, fast, reliable pastemyst API wrapper written in Golang. Official pastemyst API docs found here.

Dec 12, 2022
Simple no frills AWS S3 Golang Library using REST with V4 Signing (without AWS Go SDK)

simples3 : Simple no frills AWS S3 Library using REST with V4 Signing Overview SimpleS3 is a golang library for uploading and deleting objects on S3 b

Nov 4, 2022
Simple RESTful API for WhatsApp in Golang (using the Whatsmeow multi device library)
Simple RESTful API for WhatsApp in Golang (using the Whatsmeow multi device library)

WUZAPI WuzAPI is an implementation of @tulir/whatsmeow library as a simple RESTful API service with multiple device support and concurrent sessions. W

Dec 30, 2022
Empty an S3 bucket as fast as possible 💨

s3-destroyer Iteratively calls ListObjects, add objects keys to a buffer and calls DeleteObject in goroutines. Usage -access-key string s3 a

Dec 13, 2021
"there" also called "GoThere" aims to be a simple Go Library to reduce redundant code for REST APIs.

there "there" also called "GoThere" aims to be a simple Go Library to reduce redundant code for REST APIs. Despite the existence of the other librarie

Dec 25, 2022
Simples3 : Simple no frills AWS S3 Library using REST with V4 Signing

simples3 : Simple no frills AWS S3 Library using REST with V4 Signing Overview SimpleS3 is a golang library for uploading and deleting objects on S3 b

Nov 30, 2021
Simple-api - Create Simple `Hello World` APIs from Various Programming Languages

simple-api Create Simple `Hello World` APIs from Various Programming Languages.

Jan 18, 2022
TODO_GO: a simple todo API created in Golang with a minimum number of dependencies and configuration
TODO_GO: a simple todo API created in Golang with a minimum number of dependencies and configuration

TODO_GO TODO_GO is a simple todo API created in Golang with a minimum number of

Jan 1, 2022
A simple Go utility to display track information from, and send commands to, spotifyd from Tiling Window Managers like Sway and i3
A simple Go utility to display track information from, and send commands to, spotifyd from Tiling Window Managers like Sway and i3

Untitled Spotifyd Controller A simple Go utility to display track information from, and send commands to, spotifyd from Tiling Window Managers like Sw

Mar 8, 2022
a Go (Golang) MusicBrainz WS2 client library - work in progress
a Go (Golang) MusicBrainz WS2 client library - work in progress

gomusicbrainz a Go (Golang) MusicBrainz WS2 client library - a work in progress. Current state Currently GoMusicBrainz provides methods to perform sea

Sep 28, 2022
golang library for textbelt.com

textbelt.com API for GO This library sends text messages to mobile phones using http://textbelt.com Installing go get $ go get gopkg.in/dietsche/textb

Nov 15, 2022
Our library to use the sendcloud API endpoints in golang.

gosendcloud With this library it should be possible to interact with the endpoints of the sendcloud API via golang functions. Since we can not impleme

Dec 3, 2021
A Golang Client Library for building Cosmos SDK chain clients

Cosmos Client Lib in Go This is the start of ideas around how to implement the cosmos client libraries in a seperate repo How to instantiate and use t

Jan 6, 2023
The library parse the system netrc file for golang

gonetrc This is the library parse the system netrc file, support linux/macos/win

Jul 8, 2022
Opensea-go - Golang's library for OpenSea APIs

opensea-go Golang's library for OpenSea APIs (https://docs.opensea.io/reference)

Nov 26, 2022
G-array is a GoLang library, that contains the generic function to do the array operations.

G-array Garray is a library written in Go (Golang). Which have a collection of functions to do the array operations. Installation To install G-array p

Oct 3, 2022
Simple golang airtable API wrapper

Golang Airtable API A simple #golang package to access the Airtable API. Table of contents Golang Airtable API Table of contents Installation Basic us

Jan 5, 2023