GO语言如何实现批量压缩图片和水印

也许你要早上七点起床,晚上十二点睡觉,日复一日,踽踽独行。但只要笃定而动情地活着,即使生不逢时,你人生最坏的结果,也只是大器晚成。

前段时间想做个图片站,就用手机照了很多相片,但是要一个个用PS去压缩修改尺寸太麻烦了。最后想到了用golang去实现,算是边学边练吧。其中用到了github.com/nfnt/resize这个第三方库,仅仅支持JPG图片格式。


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!")
}
// 执行操作
func cmd(path string) {
files, _ := ioutil.ReadDir(path)
for _, file := range files {
if file.IsDir() {
fmt.Println("目录" + file.Name())
cmd(path + file.Name() + "/")
} else {
if strings.Contains(strings.ToLower(file.Name()), ".jpg") {
// 随机名称
to := path + random_name() + ".jpg"
origin := path + file.Name()
fmt.Println("正在处理" + origin + ">>>" + to)
cmd_resize(origin, 2048, 0, to)
defer os.Remove(origin)
}
}
}
}
// 改变大小
func cmd_resize(file string, width uint, height uint, to string) {
// 打开图片并解码
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) {
// 打开图片并解码
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})
}
// 水印
func cmd_watermark(file string, to string) {
// 打开图片并解码
file_origin, _ := os.Open(file)
origin, _ := jpeg.Decode(file_origin)
defer file_origin.Close()
// 打开水印图并解码
file_watermark, _ := os.Open("watermark.png")
watermark, _ := png.Decode(file_watermark)
defer file_watermark.Close()
//原始图界限
origin_size := origin.Bounds()
//创建新图层
canvas := image.NewNRGBA(origin_size)
// 贴原始图
draw.Draw(canvas, origin_size, origin, image.ZP, draw.Src)
// 贴水印图
draw.Draw(canvas, watermark.Bounds().Add(image.Pt(30, 30)), watermark, image.ZP, draw.Over)
//生成新图片
create_image, _ := os.Create(to)
jpeg.Encode(create_image, canvas, &jpeg.Options{95})
defer create_image.Close()
}
// 随机生成文件名
func random_name() string {
rand.Seed(time.Now().UnixNano())
return strconv.Itoa(rand.Int())
}

以上就是本文所述的全部内容了,希望能够对大家熟练掌握go语言有所帮助。

标签: 如何实现