bytes, file, Buffer, ReadCloser, image相互转换 - Tue, May 7, 2019
bytes,file,readCloser,image相互转换
// 打开文件
file, err := os.Open(in.Path)
// 文件转bytes
bytes, err := ioutil.ReadAll(file)
// 文件转image
img, format, err := image.Decode(file)
// bytes转Buffer
buffer := bytes.NewBuffer(bytes)
// buffer to bytes
bytes := buffer.Bytes()
// Buffer转ReadCloser
rc := ioutil.NopCloser(buffer)
// buffer to image
img, format, err := image.Decode(buffer)
// ReadCloser to image
img, format, err := image.Decode(rc)
// image to buffer
err := jpeg.Encode(buffer, image, nil)