Go language to implement batch compress picture and Watermark _golang

Source: Internet
Author: User
Tags rand

Some time ago want to do a picture station, on the phone took a lot of photos, but to use PS to compress the size of the change is too much trouble. Finally thought of using Golang to achieve, is to learn while practicing it. It uses Github.com/nfnt/resize, the third-party library, to support only JPG image formats.

Copy Code code as follows:

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 an action
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 ("processing" + Origin + ">>>" + to)
Cmd_resize (origin, 2048, 0, to)
Defer OS. Remove (Origin)
}
}
}
}
Change size
Func cmd_resize (file string, width uint, height uint, to string) {
Open the picture 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, 360, strings. Replace (To, ". jpg", "@small. jpg", 1))
}
Func cmd_thumbnail (file string, width uint, height uint, to string) {
Open the picture 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})
}
Watermarking
Func Cmd_watermark (file string, to String) {
Open the picture and decode it
File_origin, _: = OS. Open (file)
Origin, _: = JPEG. Decode (File_origin)
Defer File_origin. Close ()
Open a watermark map and decode it
File_watermark, _: = OS. Open ("Watermark.png")
Watermark, _: = png. Decode (File_watermark)
Defer File_watermark. Close ()
Original chart boundary
Origin_size: = Origin. Bounds ()
Create a new layer
Canvas: = image. Newnrgba (Origin_size)
Paste Original Map
Draw. Draw (Canvas, Origin_size, origin, image. ZP, Draw. SRC)
Paste a watermark Map
Draw. Draw (canvas, Watermark. Bounds (). ADD (image. Pt (), watermark, image. ZP, Draw. Over)
Generate a new picture
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 ())
}

The above is the full content of this article, I hope to be able to master the Go language help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.