This is a creation in Article, where the information may have evolved or changed.
Today, Golang generated thumbnails of the relevant knowledge, on the internet to find a good blog, blog address: http://www.cnblogs.com/ghj1976/p/3445507.html.
Here's a note to remind you:
Official Documents Quote:
The image interface can be obtained by calling such as Newrgba and newpaletted functions, or by calling the Decode function to decode an input stream containing image data in GIF, JPEG, or PNG format. The decoding function of the corresponding type must be registered before any specific image type can be decoded. The registration process is typically a side-effect of package initialization and is placed in the INIT function of the package. Therefore, to decode the JPG image, simply embed the following code in the program's main package:
Import _ "Image/jpg"
_ means importing the package without using the variables/functions/types in the package, just for the side effects of the package initialization function.
Tidy up the code:
Package Mainimport ("Code.google.com/p/graphics-go/graphics" "Image"_"Image/jpeg" //must import, otherwise it will appear: unknown format, other similar "Image/png" "Log" "OS")//Read fileFunc LoadImage (pathstring) (imgImage. Image, errError) {file, err: = OS. Open (PATH)ifErr! = Nil {Log. Fatal ("Open file fail:", err)return} deferfile. Close () img, _, Err =Image. Decode (file)//Decode picture return}//Save fileFunc SaveImage (pathstringImgImage. Image) (ErrError{imgfile, err: = OS. Create (path) defer imgfile. Close () Err = png. Encode (Imgfile, IMG)//coded image ifErr! = Nil {Log. Fatal ("Save fail:", err)}return}func Main () {src, err: = LoadImage ("1.jpg")ifErr! = Nil {Log. Fatal ("Open fail:", err)} DST: =Image. Newrgba (Image. Rect (0,0, -, -) Err = graphics. Scale (DST, SRC)//Zoom out picture ifErr! = Nil {Log. Fatal (Err)} saveimage ("Thumbnailimg.png", DST)}
Copyright NOTICE: Reproduced Please indicate the source of the blog, thank you.