image-processing - 去调整图像大小

标签 image-processing go

我在这里使用 Go resize 包:https://github.com/nfnt/resize

  1. 我正在从 S3 中提取图像,如下所示:

    image_data, err := mybucket.Get(key)
    // this gives me data []byte
    
  2. 之后,我需要调整图像大小:

    new_image := resize.Resize(160, 0, original_image, resize.Lanczos3)
    // problem is that the original_image has to be of type image.Image
    
  3. 将图像上传到我的 S3 存储桶

    err : = mybucket.Put('newpath', new_image, 'image/jpg', 'aclstring')
    // problem is that new image needs to be data []byte
    

如何将数据 []byte 转换为 ---> image.Image 并返回 ----> 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

关于image-processing - 去调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22940724/

相关文章:

opencv - 图像处理中的 anchor

c - YUV420 到 RGB 颜色转换错误

python-3.x - 为什么不能在 OpenCV (Python) 中使用 `cv2.cv.BoxPoints`?

go - 有没有办法使用 Go 的 `flag` 包捕获所有无法识别的命令行标志?

go - 播放完音频后如何使程序自动退出

python - 如何在 python 中以更快的方式迭代图像像素?

c++ - 在 ARM NEON 中的数组边界上加载 vector

amazon-web-services - 从 AWS SES 发送电子邮件时出现凭据错误

Go包导入了self,并且在GOPATH中找不到自己

generics - 如何获取 map 的key