Golang image processing, clipping, base64 data conversion, file storage

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Auth:philo Version:2

This article mainly introduces:

    1. Read and write picture files.
    2. How images are converted to and from Base64 in the go cache
    3. Picture clipping

In this article, all error judgments are removed for easy viewing

Base64-File

ddd, _ := base64.StdEncoding.DecodeString(datasource) //成图片文件并把文件写入到buffererr2 := ioutil.WriteFile("./output.jpg", ddd, 0666)   //buffer输出到jpg文件中(不做处理,直接写到文件)

datasourceBase64 string

Base64, Buffer

ddd, _ := base64.StdEncoding.DecodeString(datasource) //成图片文件并把文件写入到bufferbbb := bytes.NewBuffer(ddd)                           // 必须加一个buffer 不然没有read方法就会报错

After converting to buffer, there is the reader method. To be decode by the image API

Buffer-> Imagebuff (picture clipping, code connection above)

m, _, _ := image.Decode(bbb)                                       // 图片文件解码rgbImg := m.(*image.YCbCr)subImg := rgbImg.SubImage(image.Rect(0, 0, 200, 200)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1

IMG-File (code above)

f, _ := os.Create("test.jpg")     //创建文件defer f.Close()                   //关闭文件jpeg.Encode(f, subImg, nil)       //写入文件

IMG-Base64 (code above)

emptyBuff := bytes.NewBuffer(nil)                  //开辟一个新的空buffjpeg.Encode(emptyBuff, subImg, nil)                //img写入到buffdist := make([]byte, 50000)                        //开辟存储空间base64.StdEncoding.Encode(dist, emptyBuff.Bytes()) //buff转成base64fmt.Println(string(dist))                          //输出图片base64(type = []byte)_ = ioutil.WriteFile("./base64pic.txt", dist, 0666) //buffer输出到jpg文件中(不做处理,直接写到文件)

Imgfile-Base64

ff, _ := ioutil.ReadFile("output2.jpg")               //我还是喜欢用这个快速读文件bufstore := make([]byte, 5000000)                     //数据缓存base64.StdEncoding.Encode(bufstore, ff)               // 文件转base64_ = ioutil.WriteFile("./output2.jpg.txt", dist, 0666) //直接写入到文件就ok完活了。

That's probably the code. Basically some small sites are enough.
Scaling something can be preceded by the front end. A cut on the back end is enough.

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.