Golang : Convert PNG transparent background image to JPG or JPEG image
For this tutorial, we will learn how to convert PNG image file to JPEG image file. Because JPEG format does not support transparent background, you will have to expect some quality loss when converting from PNG to JPEG image.
A PNG file with transparent background
Converting PNG to JPEG in straight forward manner will cause the transparent background to become black in the final result.
A converted JPEG file with black background
We need to take extra steps to create a new image with white background and paste the PNG image over the new image before saving as JPEG.
Here you go!
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"os"
)
func main() {
pngImgFile, err := os.Open("./PNG-file.png")
if err != nil {
fmt.Println("PNG-file.png file not found!")
os.Exit(1)
}
defer pngImgFile.Close()
// create image from PNG file
imgSrc, err := png.Decode(pngImgFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// create a new Image with the same dimension of PNG image
newImg := image.NewRGBA(imgSrc.Bounds())
// we will use white background to replace PNG's transparent background
// you can change it to whichever color you want with
// a new color.RGBA{} and use image.NewUniform(color.RGBA{<fill in color>}) function
draw.Draw(newImg, newImg.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
// paste PNG image OVER to newImage
draw.Draw(newImg, newImg.Bounds(), imgSrc, imgSrc.Bounds().Min, draw.Over)
// create new out JPEG file
jpgImgFile, err := os.Create("./JPEG-file.jpg")
if err != nil {
fmt.Println("Cannot create JPEG-file.jpg !")
fmt.Println(err)
os.Exit(1)
}
defer jpgImgFile.Close()
var opt jpeg.Options
opt.Quality = 80
// convert newImage to JPEG encoded byte and save to jpgImgFile
// with quality = 80
err = jpeg.Encode(jpgImgFile, newImg, &opt)
//err = jpeg.Encode(jpgImgFile, newImg, nil) -- use nil if ignore quality options
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Converted PNG file to JPEG file")
}
Final result. A proper JPEG file with white background.
JPEG file with white background converted from PNG
References :
https://golang.org/pkg/image/draw/#Draw
https://www.socketloop.com/tutorials/golang-save-image-to-png-jpeg-or-gif-format
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+48k Golang : How to convert JSON string to map and slice
+20.1k Golang : How to get struct tag and use field name to retrieve data?
+8.9k Golang : Get SPF and DMARC from email headers to fight spam
+17.5k Golang : Find smallest number in array
+14k Golang : Reverse IP address for reverse DNS lookup example
+8.4k Golang : Ackermann function example
+9.4k Golang : Get all countries currencies code in JSON format
+5.7k Unix/Linux/MacOSx : Get local IP address
+7.8k Javascript : How to check a browser's Do Not Track status?
+52.4k Golang : How to get struct field and value by name
+5.6k Unix/Linux : How to find out the hard disk size?
+23.8k Golang : Use regular expression to validate domain name