Go Resizing Images

前端 未结 3 1184
南笙
南笙 2021-01-30 14:23

I am using the Go resize package here: https://github.com/nfnt/resize

  1. I am pulling an Image from S3, as such:

    image_data, err := mybucket.Get(ke         
    
    
            
相关标签:
3条回答
  • 2021-01-30 15:09

    Want to do it 29 times faster? Try amazing vipsthumbnail instead:

    sudo apt-get install libvips-tools
    vipsthumbnail --help-all
    

    This will resize and nicely crop saving result to a file:

    vipsthumbnail original.jpg -s 700x200 -o 700x200.jpg -c
    

    Calling from 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()
    }
    
    0 讨论(0)
  • 2021-01-30 15:10

    You could use bimg, which is powered by libvips (a fast image processing library written in C).

    If you are looking for a image resizing solution as a service, take a look to imaginary

    0 讨论(0)
  • 2021-01-30 15:14

    Read 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
    
    0 讨论(0)
提交回复
热议问题