GO language to achieve bulk compression of images and watermarks
- 2020-05-30 20:19:31
- OfStack
Some time ago, when I wanted to make a photo station, I took a lot of photos with my mobile phone. However, it was too troublesome to use PS to compress and modify the size. Finally, I came up with the idea of using golang to realize it. github.com /nfnt/resize, a third library, only supports the JPG image format.
package main
import (
"fmt"
"github.com/nfnt/resize"
"image"
"image/draw"
"image/jpeg"
"image/png"
"io/ioutil"
"log"
"math/rand"
"os"
"runtime"
"strconv"
"strings"
"time"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
cmd("data/")
fmt.Println("OK!")
}
// Perform operations
func cmd(path string) {
files, _ := ioutil.ReadDir(path)
for _, file := range files {
if file.IsDir() {
fmt.Println(" directory " + file.Name())
cmd(path + file.Name() + "/")
} else {
if strings.Contains(strings.ToLower(file.Name()), ".jpg") {
// Random name
to := path + random_name() + ".jpg"
origin := path + file.Name()
fmt.Println(" Are dealing with " + origin + ">>>" + to)
cmd_resize(origin, 2048, 0, to)
defer os.Remove(origin)
}
}
}
}
// Change the size
func cmd_resize(file string, width uint, height uint, to string) {
// Open the image and decode it
file_origin, _ := os.Open(file)
origin, _ := jpeg.Decode(file_origin)
defer file_origin.Close()
canvas := resize.Resize(width, height, origin, resize.Lanczos3)
file_out, err := os.Create(to)
if err != nil {
log.Fatal(err)
}
defer file_out.Close()
jpeg.Encode(file_out, canvas, &jpeg.Options{80})
// cmd_watermark(to, strings.Replace(to, ".jpg", "@big.jpg", 1))
cmd_thumbnail(to, 480, 360, strings.Replace(to, ".jpg", "@small.jpg", 1))
}
func cmd_thumbnail(file string, width uint, height uint, to string) {
// Open the image and decode it
file_origin, _ := os.Open(file)
origin, _ := jpeg.Decode(file_origin)
defer file_origin.Close()
canvas := resize.Thumbnail(width, height, origin, resize.Lanczos3)
file_out, err := os.Create(to)
if err != nil {
log.Fatal(err)
}
defer file_out.Close()
jpeg.Encode(file_out, canvas, &jpeg.Options{80})
}
// The watermark
func cmd_watermark(file string, to string) {
// Open the image and decode it
file_origin, _ := os.Open(file)
origin, _ := jpeg.Decode(file_origin)
defer file_origin.Close()
// Open the watermark and decode it
file_watermark, _ := os.Open("watermark.png")
watermark, _ := png.Decode(file_watermark)
defer file_watermark.Close()
// Original map boundary
origin_size := origin.Bounds()
// Create a new layer
canvas := image.NewNRGBA(origin_size)
// Stick the original figure
draw.Draw(canvas, origin_size, origin, image.ZP, draw.Src)
// Post watermark pictures
draw.Draw(canvas, watermark.Bounds().Add(image.Pt(30, 30)), watermark, image.ZP, draw.Over)
// Generate a new image
create_image, _ := os.Create(to)
jpeg.Encode(create_image, canvas, &jpeg.Options{95})
defer create_image.Close()
}
// Randomly generated file name
func random_name() string {
rand.Seed(time.Now().UnixNano())
return strconv.Itoa(rand.Int())
}
That's all for this article. I hope it will be helpful for you to master go.