Converting WebP Images to Other Formats with Golang

Ravi Tiwari
3 min readFeb 28, 2023

--

WebP is a relatively new image format that was created by Google in 2010. It is designed to be a more efficient replacement for JPEG and PNG files. One of the key advantages of WebP is that it can provide the same image quality as JPEG and PNG files, but with a smaller file size. However, not all devices and browsers support WebP, which means that it’s not always a viable option for web development. In this blog post, we will explore how to use Golang to convert WebP images to PNG and JPEG formats.

Getting Started

package main

import (
"flag"
"fmt"
"image/jpeg"
"image/png"
"os"
"path/filepath"

"golang.org/x/image/webp"
)

func main() {
var input string
flag.StringVar(&input, "input", "", "path to input WebP file")
flag.Parse()

if input == "" {
fmt.Println("Error: No input file specified")
os.Exit(1)
}

output := filepath.Base(input)
output = output[0:len(output)-len(filepath.Ext(output))] // Remove file extension

f, err := os.Open(input)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
defer f.Close()

img, err := webp.Decode(f)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}

// Convert to PNG
pngFile, err := os.Create(output + ".png")
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
defer pngFile.Close()

err = png.Encode(pngFile, img)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}

// Convert to JPEG
jpegFile, err := os.Create(output + ".jpeg")
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
defer jpegFile.Close()

err = jpeg.Encode(jpegFile, img, &jpeg.Options{Quality: 90})
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}

fmt.Println("Conversion completed successfully")
}

Steps to Run Script

go run webp_converter.go -input sample.webp

The first step in the conversion process is to install Golang on your computer. You can download the latest version from the official website at https://golang.org/dl/. Once Golang is installed, create a new file and name it “webp_converter.go”. Then, copy the code above into the file.

The next step is to import the necessary packages for the program. We need the “flag” package to handle command-line arguments, “fmt” for printing output to the console, “image/png” and “image/jpeg” for encoding the images to those formats, “os” for reading and writing files, and “path/filepath” to manipulate file paths.

The main function

The main function begins by defining a variable “input” that will hold the path to the WebP file that we want to convert. We use the “flag” package to parse command-line arguments and set the value of “input”. If no input file is specified, the program will print an error message and exit.

Next, we extract the base name of the input file (excluding the file extension) and store it in a variable named “output”. This will be used to generate the output file names for the converted images.

The program then attempts to open the input file using the “os.Open” function. If the file cannot be opened, the program will print an error message and exit. If the file is successfully opened, we use the “webp.Decode” function from the “golang.org/x/image/webp” package to decode the WebP image and store it in a variable named “img”. If the image cannot be decoded, the program will print an error message and exit.

Converting to PNG

The next step is to convert the decoded image to PNG format. We create a new file named “output.png” using the “os.Create” function. If the file cannot be created, the program will print an error message and exit. We then use the “png.Encode” function to encode the “img” variable to the new PNG file. If the encoding fails, the program will print an error message and exit.

Converting to JPEG

The final step is to convert the decoded image to JPEG format. We create a new file named “output.jpeg” using the “os.Create” function. If the file cannot be created, the program will print an error message and exit. We then use the “jpeg.Encode” function to encode the “img” variable to the new JPEG file. We set the quality of the JPEG image to 90 using the “jpeg.Options” struct. If the encoding fails, the program will print an error message and exit.

Conclusion

In this blog post, we have demonstrated how to use Golang to convert WebP images to PNG and JPEG formats. We used the “flag”, “fmt”, “image/png”, “image/jpeg”, “os”, and “path/filepath” packages to implement the program. We hope this post was helpful in showing you how to use Golang to handle images and convert them to different formats. Happy coding!

--

--

Ravi Tiwari
Ravi Tiwari

Written by Ravi Tiwari

Experienced hands-on CTO with 20+ years of cloud native microservices expertise, driving solutions for large-scale enterprises.

No responses yet