Image processing 调整图像大小

Image processing 调整图像大小,image-processing,go,Image Processing,Go,我在这里使用Go 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 ima

我在这里使用Go 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
    
  • 如何将数据
    []字节
    转换为-->
    图像。图像
    并返回到-->数据
    []字节

    读取

    想做吗?试试神奇吧:

    这将调整大小并很好地将保存结果裁剪到文件:

    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()
    }
    
    您可以使用,它由(一个用C编写的快速图像处理库)提供支持


    如果您正在寻找一种图像大小调整解决方案作为一项服务,请查看操作使用的是特定的库/包,但我认为“调整图像大小”问题可以在没有该包的情况下解决

    您可以使用
    golang.org/x/image/draw
    调整de image的大小:

    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,因为它速度更快,但看起来更糟。但还有其他方法,您可以在上看到:

    • draw.NearestNeighbor

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

    • draw.approxblinear

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

    • 绘制双线性图

      双线性是帐篷的核心。这是缓慢的,但通常会给出高质量的结果

    • draw.CatmullRom

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


    太棒了。“image/jpeg”前面的下划线是什么意思?另外,我如何使用bytes变量?最后,我如何将其编码回[]字节?非常感谢。下面的下划线是你如何导入一些只会产生副作用的东西(在本例中注册解码器)。导入时不带下划线以使用
    jpeg.Encode
    bytes
    是标准库中的一个包。如果您知道输入图像是jpeg,则可以执行
    jpeg.Decode
    (而不是
    image.Decode
    )从何处导入此
    调整大小
    包?如果不需要系统调用,还有一个问题。观察一下这种方法是否使用了可移植性是很有用的。使用纯Go可以为Go支持的每个OS/arch提供可移植性。@joonas.fi,建议使用这个库吗?@gbenroscience oops我有一个输入错误,我的意思是“失去可移植性”。我的意思是,由于它调用的是一个外部程序,所以你的围棋程序并不像纯围棋所能提供的那样独立和可移植。接受答案中的代码在这方面更好。这个答案与原始问题有什么关系?这很好,谢谢!工作起来很有魅力。
    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()
    }
    
    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)