英文:
Golang Converting image.Image to []byte
问题
遇到了将image.Image转换为[]byte的问题。问题所在的部分用虚线标出。
image_data, err := mybucket.Get(key)
if err != nil {
panic(err.Error())
}
// 将数据[]byte的格式重置为image.Image
original_image, _, err := image.Decode(bytes.NewReader(image_data))
new_image := resize.Resize(160, 0, original_image, resize.Lanczos3)
- - - - - - - - - - - - - - - - - - - - - - - - - - -
// 在这里将image.Image的格式重置为数据[]byte
var send_S3 []byte
var byteWriter = bufio.NewWriter(send_S3)
- - - - - - - - - - - - - - - - - - - - - - - - - - -
err = jpeg.Encode(byteWriter, new_image, nil)
new_path := key + "_sm"
err = mybucket.Put(new_path, send_S3, "image/jpg", "aclstring")
基本上,我需要将new_image转换为[]byte格式,以便将其发送到我的S3存储桶中。
谢谢帮助。
英文:
Having trouble converting an image.Image to []byte. The problem spot is wrapped in dotted lines.
image_data, err := mybucket.Get(key)
if err != nil {
panic(err.Error())
}
// reset format of data []byte to image.Image
original_image, _, err := image.Decode(bytes.NewReader(image_data))
new_image := resize.Resize(160, 0, original_image, resize.Lanczos3)
- - - - - - - - - - - - - - - - - - - - - - - - - - -
// reset format the image.Image to data []byte here
var send_S3 []byte
var byteWriter = bufio.NewWriter(send_S3)
- - - - - - - - - - - - - - - - - - - - - - - - - -
err = jpeg.Encode(byteWriter, new_image, nil)
new_path := key + "_sm"
err = mybucket.Put(new_path, send_S3, "image/jpg", "aclstring")
Basically, I need new_image to be in []byte format so that I can send it up to my S3 bucket.
Thanks for the help.
答案1
得分: 81
你需要使用bytes.Buffer
而不是bufio.Writer
。bytes.Buffer
用于在内存中写入数据的写入器。bufio.Writer
只是在将数据转发给另一个写入器之前将其缓存在内存中。
buf := new(bytes.Buffer)
err := jpeg.Encode(buf, new_image, nil)
send_s3 := buf.Bytes()
英文:
You want a bytes.Buffer, not a bufio.Writer. bytes.Buffer is used when you need a writer that writes to memory. bufio.Writer just caches data in memory before forwarding it to another writer.
buf := new(bytes.Buffer)
err := jpeg.Encode(buf, new_image, nil)
send_s3 := buf.Bytes()
答案2
得分: 13
在Stephen的代码基础上,如果你正在使用aws s3 go SDK来上传一个对象,你的代码应该如下所示:
// 创建缓冲区
buff := new(bytes.Buffer)
// 将图像编码到缓冲区
err = png.Encode(buff, new_image)
if err != nil {
fmt.Println("创建缓冲区失败", err)
}
// 将缓冲区转换为读取器
reader := bytes.NewReader(buff.Bytes())
// 在`PutObjectInput`中使用它
putInput := s3.PutObjectInput{
Bucket: aws.String("my_bucket"),
Body: reader,
}
// 发送请求
resp, err := svc.PutObject(&putInput)
请注意,这是一个示例代码,你需要根据自己的实际情况进行适当的修改。
英文:
Adding on to Stephen's code, if you're using the aws s3 go SDK to put an object, your code would look like:
// create buffer
buff := new(bytes.Buffer)
// encode image to buffer
err = png.Encode(buff, new_image)
if err != nil {
fmt.Println("failed to create buffer", err)
}
// convert buffer to reader
reader := bytes.NewReader(buf.Bytes())
// use it in `PutObjectInput`
putInput := s3.PutObjectInput{
Bucket: aws.String("my_bucket"),
Body: reader,
}
// send response
resp, err := svc.PutObject(&putInput)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论