[]byte to image

(Joel) #1

Hi am downloading a picture from a gc Bucket after download i end up with a [] trying to decode it in to the picture so i can manipulated it. but i keep getting a nil value. please help here is my code

//read object from bucket

rc, err := bkt.Object("1090873.jpeg").NewReader(ctx)

if err != nil {
	fmt.Println("Error getting image from bucket")

data, err := ioutil.ReadAll(rc)
if err != nil {
	fmt.Pringln("Error reading rc")

// decode []byte in to workable image

img, _, _ := image.Decode(bytes.NewReader(data))

if img == nil {
	fmt.Println("Your don't have an image body")

i have tried variations of this func but not sure if am using this right obviously not since is not working.
img, _, _ := image.Decode(bytes.NewReader(data))
am sure i have data is not nill cause i can print the value of the []byte array to the console just don’t know how to turn it in to an image. My next line of code would be:
// Resize the cropped image to width = 500px preserving the aspect ratio.
img = imaging.Resize(src, 500, 0, imaging.Lanczos)

But to get here i need an image.

(Joel) #2

Here is what i ended up with feel free to improve it. thanks.

func serveFrames(imgByte []byte) image.Image {

img, _, err := image.Decode(bytes.NewReader(imgByte))
if err != nil {

out, _ := os.Create("./img.jpeg")
defer out.Close()

var opts jpeg.Options
opts.Quality = 1

err = jpeg.Encode(out, img, &opts)

if err != nil {
return img


(Gonzalo Sayago) #3

I am using libvips to process images. Try https://github.com/davidbyttow/govips !