duancoubeng5909 2014-04-08 18:36
浏览 6919
已采纳

Golang将image.Image转换为[] byte

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.

  • 写回答

2条回答 默认 最新

  • douman6679 2014-04-08 18:44
    关注

    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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于螺旋焊缝的图像处理
  • ¥15 qt 散点图自定义图形
  • ¥100 将USDZ文件转化为带颜色的OBJ文件
  • ¥15 对象代号: , 表单: 不存在!
  • ¥15 WebSocket的问题
  • ¥15 centos上启动kylin后网页报错404
  • ¥20 使用hackrf进行信号收发时接收到的信号幅度太小
  • ¥15 WebSocket的问题
  • ¥15 BDSBAS-B1C和B1C信号有什么不同
  • ¥15 在半圆平面内随机生成点坐标