调整图像大小

问题描述 投票:0回答:5

我在这里使用Go调整大小包: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
再转换回 ----> 数据
[]byte

image-processing go
5个回答
66
投票

阅读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

49
投票

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

    NearestNeighbor 是最近邻插值器。它非常快,但通常给出的结果质量非常低。放大时,结果会看起来“块状”。

  • draw.ApproxBiLinear

    ApproxBiLinear 是最近邻插值器和双线性插值器的混合体。它速度很快,但通常会给出中等质量的结果。

  • draw.BiLinear

    BiLinear 是帐篷内核。它很慢,但通常会给出高质量的结果。

  • draw.CatmullRom

    CatmullRom 是 Catmull-Rom 内核。它非常慢,但通常会给出非常高质量的结果。


9
投票

想要更快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()
}

4
投票

您可以使用 bimg,它由 libvips(用 C 编写的快速图像处理库)提供支持。

如果您正在寻找图像调整大小解决方案作为服务,请查看imaginary


0
投票

无第三方软件包。以下代码可以将图像尺寸减小到最小 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
}
© www.soinside.com 2019 - 2024. All rights reserved.