Golang : Save image to PNG, JPEG or GIF format.
This tutorial will demonstrate how to save images into files. We will learn how to generate some random images and save the content from memory into PNG, JPG and GIF file. Basically, the codes are almost the same except for the slight variants to demonstrate how to use the Encode
functions.
JPG :
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg"
"math/rand"
"os"
"time"
)
func main() {
flag.Parse()
rand.Seed(time.Now().UTC().UnixNano())
out, err := os.Create("./output.jpg")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// generate some QR code look a like image
imgRect := image.Rect(0, 0, 100, 100)
img := image.NewGray(imgRect)
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
for y := 0; y < 100; y += 10 {
for x := 0; x < 100; x += 10 {
fill := &image.Uniform{color.Black}
if rand.Intn(10)%2 == 0 {
fill = &image.Uniform{color.White}
}
draw.Draw(img, image.Rect(x, y, x+10, y+10), fill, image.ZP, draw.Src)
}
}
var opt jpeg.Options
opt.Quality = 80
// ok, write out the data into the new JPEG file
err = jpeg.Encode(out, img, &opt) // put quality to 80%
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Generated image to output.jpg \n")
}
PNG :
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"math/rand"
"os"
"time"
)
func main() {
flag.Parse()
rand.Seed(time.Now().UTC().UnixNano())
out, err := os.Create("./output.png")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// generate some QR code look a like image
imgRect := image.Rect(0, 0, 100, 100)
img := image.NewGray(imgRect)
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
for y := 0; y < 100; y += 10 {
for x := 0; x < 100; x += 10 {
fill := &image.Uniform{color.Black}
if rand.Intn(10)%2 == 0 {
fill = &image.Uniform{color.White}
}
draw.Draw(img, image.Rect(x, y, x+10, y+10), fill, image.ZP, draw.Src)
}
}
// ok, write out the data into the new PNG file
err = png.Encode(out, img)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Generated image to output.png \n")
}
GIF :
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/gif"
"math/rand"
"os"
"time"
)
func main() {
flag.Parse()
rand.Seed(time.Now().UTC().UnixNano())
out, err := os.Create("./output.gif")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// generate some QR code look a like image
imgRect := image.Rect(0, 0, 100, 100)
img := image.NewGray(imgRect)
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
for y := 0; y < 100; y += 10 {
for x := 0; x < 100; x += 10 {
fill := &image.Uniform{color.Black}
if rand.Intn(10)%2 == 0 {
fill = &image.Uniform{color.White}
}
draw.Draw(img, image.Rect(x, y, x+10, y+10), fill, image.ZP, draw.Src)
}
}
var opt gif.Options
opt.NumColors = 256 // you can add more parameters if you want
// ok, write out the data into the new GIF file
err = gif.Encode(out, img, &opt) // put num of colors to 256
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Generated image to output.gif \n")
}
Hope this simple tutorial will be able to assist you in dealing with popular image file formats.
See also : Golang : How to read JPG(JPEG), GIF and PNG files ?
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
+11.3k Golang : Delay or limit HTTP requests example
+5.6k Unix/Linux/MacOSx : Get local IP address
+5.7k Javascript : How to replace HTML inside <div>?
+8k Golang : Multiplexer with net/http and map
+27.3k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+20.4k Nginx + FastCGI + Go Setup.
+9.4k Golang : Changing a RGBA image number of channels with OpenCV
+6.5k Golang : Warp text string by number of characters or runes example
+11.3k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+10k Golang : Find and replace data in all files recursively
+22k Golang : How to run Golang application such as web server in the background or as daemon?
+79.1k Golang : How to return HTTP status code?