[]byte to image

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.

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 {
	log.Fatalln(err)
}

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

var opts jpeg.Options
opts.Quality = 1

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

if err != nil {
	log.Println(err)
}
return img

}

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

you are on the right track with your approach. The issue might be with error handling or image format compatibility.

You can try this script.

func serveFrames(imgByte []byte) image.Image {
    img, _, err := image.Decode(bytes.NewReader(imgByte))
    if err != nil {
        log.Fatalf("Error decoding image: %v", err)
    }

    // Resize the image
    resizedImg := imaging.Resize(img, 500, 0, imaging.Lanczos)

    // Saving the resized image (optional)
    out, err := os.Create("./resized_img.jpeg")
    if err != nil {
        log.Fatalf("Error creating image file: %v", err)
    }
    defer out.Close()

    var opts jpeg.Options
    opts.Quality = 90 // Adjust quality as needed

    err = jpeg.Encode(out, resizedImg, &opts)
    if err != nil {
        log.Fatalf("Error encoding image: %v", err)
    }

    return resizedImg
}