Golang thumbnail image

Source: Internet
Author: User
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.

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.