本文介绍了调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里使用Go调整大小包: https://github.com/nfnt/resize
I am using the Go resize package here: https://github.com/nfnt/resize
-
我正在从S3中提取图像,例如:
I am pulling an Image from S3, as such:
image_data, err := mybucket.Get(key)
// this gives me data []byte
在那之后,我需要调整图像的大小:
After that, I need to resize the image:
new_image := resize.Resize(160, 0, original_image, resize.Lanczos3)
// problem is that the original_image has to be of type image.Image
将图像上传到我的S3存储桶
Upload the image to my S3 bucket
err : = mybucket.Put('newpath', new_image, 'image/jpg', 'aclstring')
// problem is that new image needs to be data []byte
如何将数据[]byte
转换为---> image.Image
并返回到---->数据[]byte
?
How do I transform a data []byte
to ---> image.Image
and back to ----> data []byte
?
推荐答案
阅读 http://golang.org/pkg/image
// you need the image package, and a format package for encoding/decoding
import (
"bytes"
"image"
"image/jpeg" // if you don't need to use jpeg.Encode, use this line instead
// _ "image/jpeg"
"github.com/nfnt/resize"
)
// Decoding gives you an Image.
// If you have an io.Reader already, you can give that to Decode
// without reading it into a []byte.
image, _, err := image.Decode(bytes.NewReader(data))
// check err
newImage := resize.Resize(160, 0, original_image, resize.Lanczos3)
// Encode uses a Writer, use a Buffer if you need the raw []byte
err = jpeg.Encode(someWriter, newImage, nil)
// check err
这篇关于调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!