Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go Resizing Images

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(key) // this gives me data []byte 
  2. After that, I need to resize the image:

    new_image := resize.Resize(160, 0, original_image, resize.Lanczos3) // problem is that the original_image has to be of type image.Image 
  3. Upload the image to my S3 bucket

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

How do I transform a data []byte to ---> image.Image and back to ----> data []byte?

like image 674
Jorge Olivero Avatar asked Apr 08 '14 14:04

Jorge Olivero


People also ask

Is Golang good for image processing?

But Golang also has its own way to image processing, One of good thing in Golang is it's powerful standard packages, when come to images, image package (https://golang.org/pkg/image/) consists all ground level tools that need to play with images.


2 Answers

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 
like image 101
JimB Avatar answered Sep 21 '22 06:09

JimB


The OP is using a specific library/package, but I think that the issue of "Go Resizing Images" can be solved without that package.

You can resize de image using 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) 

In that case I choose draw.NearestNeighbor, because it's faster, but looks worse. but there's other methods, you can see on https://pkg.go.dev/golang.org/x/image/draw#pkg-variables:

  • draw.NearestNeighbor
    NearestNeighbor is the nearest neighbor interpolator. It is very fast, but usually gives very low quality results. When scaling up, the result will look 'blocky'.

  • draw.ApproxBiLinear
    ApproxBiLinear is a mixture of the nearest neighbor and bi-linear interpolators. It is fast, but usually gives medium quality results.

  • draw.BiLinear
    BiLinear is the tent kernel. It is slow, but usually gives high quality results.

  • draw.CatmullRom
    CatmullRom is the Catmull-Rom kernel. It is very slow, but usually gives very high quality results.

like image 39
Inkeliz Avatar answered Sep 23 '22 06:09

Inkeliz