我在这里使用Go调整大小包:https://github.com/nfnt/resize
我正在从 S3 中提取图像,如下所示:
image_data, err := mybucket.Get(key)
// this gives me data []byte
之后,我需要调整图像大小:
new_image := resize.Resize(160, 0, original_image, resize.Lanczos3)
// problem is that the original_image has to be of type image.Image
将图像上传到我的 S3 存储桶
err : = mybucket.Put('newpath', new_image, 'image/jpg', 'aclstring')
// problem is that new image needs to be data []byte
如何将数据
[]byte
转换为 ---> image.Image
再转换回 ----> 数据 []byte
?
// 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
OP正在使用特定的库/包,但我认为“调整图像大小”的问题可以在没有该包的情况下解决。
您可以使用
golang.org/x/image/draw
调整图像大小:
input, _ := os.Open("your_image.png")
defer input.Close()
output, _ := os.Create("your_image_resized.png")
defer output.Close()
// Decode the image (from PNG to image.Image):
src, _ := png.Decode(input)
// Set the expected size that you want:
dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Max.X/2, src.Bounds().Max.Y/2))
// Resize:
draw.NearestNeighbor.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)
// Encode to `output`:
png.Encode(output, dst)
在这种情况下我选择
draw.NearestNeighbor
,因为它更快,但看起来更糟。但还有其他方法,你可以在 https://pkg.go.dev/golang.org/x/image/draw#pkg-variables: 上看到
draw.NearestNeighbor
draw.ApproxBiLinear
draw.BiLinear
draw.CatmullRom
想要更快29倍吗?尝试一下神奇的
vipsthumbnail
:
sudo apt-get install libvips-tools
vipsthumbnail --help-all
这将调整大小并很好地将结果裁剪到文件中:
vipsthumbnail original.jpg -s 700x200 -o 700x200.jpg -c
从 Go 调用:
func resizeExternally(from string, to string, width uint, height uint) error {
var args = []string{
"--size", strconv.FormatUint(uint64(width), 10) + "x" +
strconv.FormatUint(uint64(height), 10),
"--output", to,
"--crop",
from,
}
path, err := exec.LookPath("vipsthumbnail")
if err != nil {
return err
}
cmd := exec.Command(path, args...)
return cmd.Run()
}
无第三方软件包。以下代码可以将图像尺寸减小到最小 50px 高度。
func resizeImage(img image.RGBA, height int) image.RGBA {
if height < 50 {
return img
}
bounds := img.Bounds()
imgHeight := bounds.Dy()
if height > imgHeight {
return img
}
imgWidth := bounds.Dx()
resizeFactor := float32(imgHeight) / float32(height)
ratio := float32(imgWidth) / float32(imgHeight)
width := int(float32(height) * ratio)
resizedImage := image.NewRGBA(image.Rect(0, 0, width, height))
var imgX, imgY int
var imgColor color.Color
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
imgX = int(resizeFactor*float32(x) + 0.5)
imgY = int(resizeFactor*float32(y) + 0.5)
imgColor = img.At(imgX, imgY)
resizedImage.Set(x, y, imgColor)
}
}
return *resizedImage
}